SDL: windows: SDL_IMMDevice needed more deinit code from the Win32 path.

From fff34f630479f79cc58f0b3455969c3ddf32d5d0 Mon Sep 17 00:00:00 2001
From: Ethan Lee <[EMAIL REDACTED]>
Date: Fri, 15 Jul 2022 09:46:53 -0400
Subject: [PATCH] windows: SDL_IMMDevice needed more deinit code from the Win32
 path.

Fixes #5919
---
 src/audio/wasapi/SDL_wasapi.c         | 19 ------------------
 src/audio/wasapi/SDL_wasapi_win32.c   |  4 +---
 src/audio/wasapi/SDL_wasapi_winrt.cpp | 28 ++++++++++++++++++---------
 src/core/windows/SDL_immdevice.c      | 12 ++++++++++++
 4 files changed, 32 insertions(+), 31 deletions(-)

diff --git a/src/audio/wasapi/SDL_wasapi.c b/src/audio/wasapi/SDL_wasapi.c
index 5b6b8216483..12236110a03 100644
--- a/src/audio/wasapi/SDL_wasapi.c
+++ b/src/audio/wasapi/SDL_wasapi.c
@@ -45,15 +45,6 @@
 #define AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM 0x80000000
 #endif
 
-/* This is a list of device id strings we have inflight, so we have consistent pointers to the same device. */
-typedef struct DevIdList
-{
-    WCHAR *str;
-    struct DevIdList *next;
-} DevIdList;
-
-static DevIdList *deviceid_list = NULL;
-
 /* Some GUIDs we need to know without linking to libraries that aren't available before Vista. */
 static const IID SDL_IID_IAudioRenderClient = { 0xf294acfc, 0x3146, 0x4483,{ 0xa7, 0xbf, 0xad, 0xdc, 0xa7, 0xc2, 0x60, 0xe2 } };
 static const IID SDL_IID_IAudioCaptureClient = { 0xc8adbd64, 0xe71e, 0x48a0,{ 0xa4, 0xde, 0x18, 0x5c, 0x39, 0x5c, 0xd3, 0x17 } };
@@ -602,17 +593,7 @@ WASAPI_ThreadDeinit(_THIS)
 static void
 WASAPI_Deinitialize(void)
 {
-    DevIdList *devidlist;
-    DevIdList *next;
-
     WASAPI_PlatformDeinit();
-
-    for (devidlist = deviceid_list; devidlist; devidlist = next) {
-        next = devidlist->next;
-        SDL_free(devidlist->str);
-        SDL_free(devidlist);
-    }
-    deviceid_list = NULL;
 }
 
 static SDL_bool
diff --git a/src/audio/wasapi/SDL_wasapi_win32.c b/src/audio/wasapi/SDL_wasapi_win32.c
index d76c8037486..13f05b661d2 100644
--- a/src/audio/wasapi/SDL_wasapi_win32.c
+++ b/src/audio/wasapi/SDL_wasapi_win32.c
@@ -68,8 +68,6 @@ WASAPI_PlatformInit(void)
 void
 WASAPI_PlatformDeinit(void)
 {
-    SDL_IMMDevice_Quit();
-
     if (libavrt) {
         FreeLibrary(libavrt);
         libavrt = NULL;
@@ -78,7 +76,7 @@ WASAPI_PlatformDeinit(void)
     pAvSetMmThreadCharacteristicsW = NULL;
     pAvRevertMmThreadCharacteristics = NULL;
 
-    WIN_CoUninitialize();
+    SDL_IMMDevice_Quit();
 }
 
 void
diff --git a/src/audio/wasapi/SDL_wasapi_winrt.cpp b/src/audio/wasapi/SDL_wasapi_winrt.cpp
index acaaf0be53e..a4796f31b3d 100644
--- a/src/audio/wasapi/SDL_wasapi_winrt.cpp
+++ b/src/audio/wasapi/SDL_wasapi_winrt.cpp
@@ -62,6 +62,15 @@ extern "C" {
     SDL_atomic_t SDL_IMMDevice_DefaultCaptureGeneration;
 }
 
+/* This is a list of device id strings we have inflight, so we have consistent pointers to the same device. */
+typedef struct DevIdList
+{
+    WCHAR *str;
+    struct DevIdList *next;
+} DevIdList;
+
+static DevIdList *deviceid_list = NULL;
+
 class SDL_WasapiDeviceEventHandler
 {
 public:
@@ -204,10 +213,20 @@ int WASAPI_PlatformInit(void)
 
 void WASAPI_PlatformDeinit(void)
 {
+    DevIdList *devidlist;
+    DevIdList *next;
+
     delete playback_device_event_handler;
     playback_device_event_handler = nullptr;
     delete capture_device_event_handler;
     capture_device_event_handler = nullptr;
+
+    for (devidlist = deviceid_list; devidlist; devidlist = next) {
+        next = devidlist->next;
+        SDL_free(devidlist->str);
+        SDL_free(devidlist);
+    }
+    deviceid_list = NULL;
 }
 
 void WASAPI_EnumerateEndpoints(void)
@@ -359,15 +378,6 @@ WaveFormatToSDLFormat(WAVEFORMATEX *waveformat)
     return 0;
 }
 
-/* This is a list of device id strings we have inflight, so we have consistent pointers to the same device. */
-typedef struct DevIdList
-{
-    WCHAR *str;
-    struct DevIdList *next;
-} DevIdList;
-
-static DevIdList *deviceid_list = NULL;
-
 static void
 WASAPI_RemoveDevice(const SDL_bool iscapture, LPCWSTR devid)
 {
diff --git a/src/core/windows/SDL_immdevice.c b/src/core/windows/SDL_immdevice.c
index d626ddf79d3..01658db8b1a 100644
--- a/src/core/windows/SDL_immdevice.c
+++ b/src/core/windows/SDL_immdevice.c
@@ -346,11 +346,23 @@ SDL_IMMDevice_Init(void)
 void
 SDL_IMMDevice_Quit(void)
 {
+    DevIdList *devidlist;
+    DevIdList *next;
+
     if (enumerator) {
         IMMDeviceEnumerator_UnregisterEndpointNotificationCallback(enumerator, (IMMNotificationClient *)&notification_client);
         IMMDeviceEnumerator_Release(enumerator);
         enumerator = NULL;
     }
+
+    WIN_CoUninitialize();
+
+    for (devidlist = deviceid_list; devidlist; devidlist = next) {
+        next = devidlist->next;
+        SDL_free(devidlist->str);
+        SDL_free(devidlist);
+    }
+    deviceid_list = NULL;
 }
 
 int