]> Repos - portaudio/commitdiff
wasapi:
authordmitrykos <dmitrykos@0f58301d-fd10-0410-b4af-bbb618454e57>
Thu, 15 Apr 2010 17:44:13 +0000 (17:44 +0000)
committerdmitrykos <dmitrykos@0f58301d-fd10-0410-b4af-bbb618454e57>
Thu, 15 Apr 2010 17:44:13 +0000 (17:44 +0000)
 - added calling thread id checks to match Pa_Initialize/Pa_Uninitialize in the same calling thread, if not the case then console warning will be thrown and CoUninitialize will not be called (preferring leak over uninitializing user-side COM)

src/hostapi/wasapi/pa_win_wasapi.c

index 30901752152cf894e1a7deaa24e621385ab47b21..f659096b749c9667224dadacc66b2790b424db33 100644 (file)
@@ -545,7 +545,8 @@ static void _OnStreamStop(PaWasapiStream *stream);
 static void _FinishStream(PaWasapiStream *stream);\r
 \r
 // Local statics\r
-static volatile BOOL g_bCOMInitialized = FALSE;\r
+static volatile BOOL  g_WasapiCOMInit    = FALSE;\r
+static volatile DWORD g_WasapiInitThread = 0;\r
 \r
 // ------------------------------------------------------------------------------------------\r
 #define LogHostError(HRES) __LogHostError(HRES, __FUNCTION__, __FILE__, __LINE__)\r
@@ -957,7 +958,11 @@ PaError PaWasapi_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiInd
         return paUnanticipatedHostError;\r
        }\r
     if (hr != RPC_E_CHANGED_MODE)\r
-        g_bCOMInitialized = TRUE;\r
+        g_WasapiCOMInit = TRUE;\r
+\r
+       // Memorize calling thread id and report warning on Uninitialize if calling thread is different\r
+       // as CoInitialize must match CoUninitialize in the same thread.\r
+       g_WasapiInitThread = GetCurrentThreadId();\r
 \r
     paWasapi = (PaWasapiHostApiRepresentation *)PaUtil_AllocateMemory( sizeof(PaWasapiHostApiRepresentation) );\r
     if (paWasapi == NULL)\r
@@ -1297,9 +1302,21 @@ static void Terminate( PaUtilHostApiRepresentation *hostApi )
        // Close AVRT\r
        CloseAVRT();\r
 \r
-       // Uninit COM\r
-    if (g_bCOMInitialized)\r
-        CoUninitialize();\r
+       // Uninit COM (checking calling thread we won't unitialize user's COM if one is calling\r
+       //             Pa_Unitialize by mistake from not initializing thread)\r
+    if (g_WasapiCOMInit)\r
+       {\r
+               DWORD calling_thread_id = GetCurrentThreadId();\r
+               if (g_WasapiInitThread != calling_thread_id)\r
+               {\r
+                       PRINT(("WASAPI: failed CoUninitializes calling thread[%d] does not match initializing thread[%d]\n", \r
+                               calling_thread_id, g_WasapiInitThread));\r
+               }\r
+               else\r
+               {\r
+                       CoUninitialize();\r
+               }\r
+       }\r
 }\r
 \r
 // ------------------------------------------------------------------------------------------\r