Skip to content

Commit

Permalink
WGI: Keep a reference to the MTA to avoid crashing on COM teardown
Browse files Browse the repository at this point in the history
Fixes #5552
Fixes #5270
  • Loading branch information
cgutman authored and slouken committed Apr 21, 2022
1 parent 8982d9f commit 00b2e10
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/joystick/windows/SDL_windows_gaming_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,29 @@ WGI_JoystickInit(void)
return SDL_SetError("RoInitialize() failed");
}

#ifndef __WINRT__
{
/* There seems to be a bug in Windows where a dependency of WGI can be unloaded from memory prior to WGI itself.
* This results in Windows_Gaming_Input!GameController::~GameController() invoking an unloaded DLL and crashing.
* As a workaround, we will keep a reference to the MTA to prevent COM from unloading DLLs later.
* See https://github.com/libsdl-org/SDL/issues/5552 for more details.
*/
static PVOID cookie = NULL;
if (!cookie) {
typedef HRESULT (WINAPI *CoIncrementMTAUsage_t)(PVOID* pCookie);
CoIncrementMTAUsage_t CoIncrementMTAUsageFunc = (CoIncrementMTAUsage_t)WIN_LoadComBaseFunction("CoIncrementMTAUsage");
if (CoIncrementMTAUsageFunc) {
if (FAILED(CoIncrementMTAUsageFunc(&cookie))) {
return SDL_SetError("CoIncrementMTAUsage() failed");
}
} else {
/* CoIncrementMTAUsage() is present since Win8, so we should never make it here. */
return SDL_SetError("CoIncrementMTAUsage() not found");
}
}
}
#endif

#ifdef __WINRT__
WindowsCreateStringReferenceFunc = WindowsCreateStringReference;
RoGetActivationFactoryFunc = RoGetActivationFactory;
Expand Down

0 comments on commit 00b2e10

Please sign in to comment.