sdl2-compat: Clean up joysticks and sensors in SDL_Quit()

From bc57e09188e7956eb117dfc09662d984d0a27a38 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 11 Nov 2023 06:54:50 -0800
Subject: [PATCH] Clean up joysticks and sensors in SDL_Quit()

---
 src/sdl2_compat.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 5e3c4a1..a55fe6e 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -1352,8 +1352,6 @@ SDL_LOG_IMPL(Error, ERROR)
 SDL_LOG_IMPL(Critical, CRITICAL)
 #undef SDL_LOG_IMPL
 
-/* !!! FIXME: when we override SDL_Quit(), we need to free/reset gamepad_button_swap_list */
-
 static void UpdateGamepadButtonSwap(SDL_Gamepad *gamepad)
 {
     int i;
@@ -3624,6 +3622,25 @@ SDL_Quit(void)
     if (SDL3_WasInit(SDL_INIT_VIDEO)) {
         GestureQuit();
     }
+
+    if (joystick_list) {
+        SDL3_free(joystick_list);
+        joystick_list = NULL;
+    }
+    num_joysticks = 0;
+
+    if (sensor_list) {
+        SDL3_free(sensor_list);
+        sensor_list = NULL;
+    }
+    num_sensors = 0;
+
+    if (gamepad_button_swap_list) {
+        SDL3_free(gamepad_button_swap_list);
+        gamepad_button_swap_list = NULL;
+    }
+    num_gamepad_button_swap_list = 0;
+
     SDL3_Quit();
 }
 
@@ -5938,7 +5955,6 @@ GetJoystickInstanceFromIndex(int idx)
     return joystick_list[idx];
 }
 
-/* !!! FIXME: when we override SDL_Quit(), we need to free/reset joystick_list and friends*/
 /* !!! FIXME: put a mutex on the joystick and sensor lists. Strictly speaking, this will break if you multithread it, but it doesn't have to crash. */
 
 DECLSPEC int SDLCALL
@@ -6273,8 +6289,6 @@ SDL_JoystickDetachVirtual(int device_index)
 }
 
 
-/* !!! FIXME: when we override SDL_Quit(), we need to free/reset sensor_list */
-
 static SDL_SensorID
 GetSensorInstanceFromIndex(int idx)
 {