SDL: Fixed cleaning up dependent subsystems if initialization fails

From 5b5b1a892672a29aeab08ebee175b992d6832395 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 15 Sep 2024 11:40:31 -0700
Subject: [PATCH] Fixed cleaning up dependent subsystems if initialization
 fails

---
 src/SDL.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/SDL.c b/src/SDL.c
index 4e93d27381095..92bed3e92d9d4 100644
--- a/src/SDL.c
+++ b/src/SDL.c
@@ -333,6 +333,7 @@ SDL_bool SDL_InitSubSystem(SDL_InitFlags flags)
             SDL_IncrementSubsystemRefCount(SDL_INIT_VIDEO);
             if (!SDL_VideoInit(NULL)) {
                 SDL_DecrementSubsystemRefCount(SDL_INIT_VIDEO);
+                SDL_QuitSubSystem(SDL_INIT_EVENTS);
                 goto quit_and_error;
             }
         } else {
@@ -357,6 +358,7 @@ SDL_bool SDL_InitSubSystem(SDL_InitFlags flags)
             SDL_IncrementSubsystemRefCount(SDL_INIT_AUDIO);
             if (!SDL_InitAudio(NULL)) {
                 SDL_DecrementSubsystemRefCount(SDL_INIT_AUDIO);
+                SDL_QuitSubSystem(SDL_INIT_EVENTS);
                 goto quit_and_error;
             }
         } else {
@@ -381,6 +383,7 @@ SDL_bool SDL_InitSubSystem(SDL_InitFlags flags)
             SDL_IncrementSubsystemRefCount(SDL_INIT_JOYSTICK);
             if (!SDL_InitJoysticks()) {
                 SDL_DecrementSubsystemRefCount(SDL_INIT_JOYSTICK);
+                SDL_QuitSubSystem(SDL_INIT_EVENTS);
                 goto quit_and_error;
             }
         } else {
@@ -404,6 +407,7 @@ SDL_bool SDL_InitSubSystem(SDL_InitFlags flags)
             SDL_IncrementSubsystemRefCount(SDL_INIT_GAMEPAD);
             if (!SDL_InitGamepads()) {
                 SDL_DecrementSubsystemRefCount(SDL_INIT_GAMEPAD);
+                SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
                 goto quit_and_error;
             }
         } else {
@@ -466,6 +470,7 @@ SDL_bool SDL_InitSubSystem(SDL_InitFlags flags)
             SDL_IncrementSubsystemRefCount(SDL_INIT_CAMERA);
             if (!SDL_CameraInit(NULL)) {
                 SDL_DecrementSubsystemRefCount(SDL_INIT_CAMERA);
+                SDL_QuitSubSystem(SDL_INIT_EVENTS);
                 goto quit_and_error;
             }
         } else {