From 9d457aa44684f0b759c11e6210eabedee0ba591c Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Thu, 29 Jul 2021 14:24:40 -0700 Subject: [PATCH] Don't uninitialize COM because of what appears to be a bug in Microsoft WGI reference counting. This fixes https://github.com/libsdl-org/SDL/issues/4488 --- .../windows/SDL_windows_gaming_input.c | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/joystick/windows/SDL_windows_gaming_input.c b/src/joystick/windows/SDL_windows_gaming_input.c index d6d0e7f89..c48a56635 100644 --- a/src/joystick/windows/SDL_windows_gaming_input.c +++ b/src/joystick/windows/SDL_windows_gaming_input.c @@ -744,7 +744,51 @@ WGI_JoystickQuit(void) } SDL_zero(wgi); - WIN_CoUninitialize(); + /* Don't uninitialize COM because of what appears to be a bug in Microsoft WGI reference counting. + * + * If you plug in a non-Xbox controller and let the application run for 30 seconds, then it crashes in CoUninitialize() + * with this stack trace: + + Windows.Gaming.Input.dll!GameController::~GameController(void) Unknown + Windows.Gaming.Input.dll!GameController::`vector deleting destructor'(unsigned int) Unknown + Windows.Gaming.Input.dll!Microsoft::WRL::Details::RuntimeClassImpl,1,1,0,struct Windows::Gaming::Input::IGameController,struct Windows::Gaming::Input::IGameControllerBatteryInfo,struct Microsoft::WRL::CloakedIid,class Microsoft::WRL::FtmBase>::Release(void) Unknown + Windows.Gaming.Input.dll!Windows::Gaming::Input::Custom::Details::AggregableRuntimeClass,struct Microsoft::WRL::CloakedIid,struct Microsoft::WRL::CloakedIid,struct Microsoft::WRL::CloakedIid,class Microsoft::WRL::Details::Nil,class Microsoft::WRL::Details::Nil,class Microsoft::WRL::Details::Nil>::Release(void) Unknown + Windows.Gaming.Input.dll!Microsoft::WRL::ComPtr<`WaitForCompletion,Windows::Foundation::IAsyncOperationWithProgress>'::`2'::FTMEventDelegate>::~ComPtr<`WaitForCompletion,Windows::Foundation::IAsyncOperationWithProgress>'::`2'::FTMEventDelegate>() Unknown + Windows.Gaming.Input.dll!`eh vector destructor iterator'(void *,unsigned int,int,void (*)(void *)) Unknown + Windows.Gaming.Input.dll!Windows::Gaming::Input::Custom::Details::GameControllerCollection::~GameControllerCollection(void) Unknown + Windows.Gaming.Input.dll!Windows::Gaming::Input::Custom::Details::GameControllerCollection::`vector deleting destructor'(unsigned int) Unknown + Windows.Gaming.Input.dll!Microsoft::WRL::Details::RuntimeClassImpl,1,1,0,struct Windows::Foundation::Collections::IIterable,struct Windows::Foundation::Collections::IVectorView,class Microsoft::WRL::FtmBase>::Release(void) Unknown + Windows.Gaming.Input.dll!Windows::Gaming::Input::Custom::Details::CustomGameControllerFactoryBase::~CustomGameControllerFactoryBase(void) Unknown + Windows.Gaming.Input.dll!Windows::Gaming::Input::Custom::Details::CustomGameControllerFactoryBase::`vector deleting destructor'(unsigned int) Unknown + Windows.Gaming.Input.dll!Microsoft::WRL::ActivationFactory >,struct Windows::Gaming::Input::IFlightStickStatics,class Microsoft::WRL::Details::Nil,0>::Release(void) Unknown + Windows.Gaming.Input.dll!Microsoft::WRL::ComPtr<`WaitForCompletion,Windows::Foundation::IAsyncOperationWithProgress>'::`2'::FTMEventDelegate>::~ComPtr<`WaitForCompletion,Windows::Foundation::IAsyncOperationWithProgress>'::`2'::FTMEventDelegate>() Unknown + Windows.Gaming.Input.dll!NtList::~NtList(void) Unknown + Windows.Gaming.Input.dll!FactoryManager::`vector deleting destructor'(unsigned int) Unknown + Windows.Gaming.Input.dll!Microsoft::WRL::ActivationFactory,struct Windows::Gaming::Input::Custom::IGameControllerFactoryManagerStatics2,struct Microsoft::WRL::CloakedIid,0>::Release(void) Unknown + Windows.Gaming.Input.dll!Microsoft::WRL::Details::TerminateMap(class Microsoft::WRL::Details::ModuleBase *,unsigned short const *,bool) Unknown + Windows.Gaming.Input.dll!Microsoft::WRL::Module<1,class Microsoft::WRL::Details::DefaultModule<1> >::~Module<1,class Microsoft::WRL::Details::DefaultModule<1> >(void) Unknown + Windows.Gaming.Input.dll!Microsoft::WRL::Details::DefaultModule<1>::`vector deleting destructor'(unsigned int) Unknown + Windows.Gaming.Input.dll!`dynamic atexit destructor for 'Microsoft::WRL::Details::StaticStorage,0,int>::instance_''() Unknown + Windows.Gaming.Input.dll!__CRT_INIT@12() Unknown + Windows.Gaming.Input.dll!__DllMainCRTStartup() Unknown + ntdll.dll!_LdrxCallInitRoutine@16() Unknown + ntdll.dll!LdrpCallInitRoutine() Unknown + ntdll.dll!LdrpProcessDetachNode() Unknown + ntdll.dll!LdrpUnloadNode() Unknown + ntdll.dll!LdrpDecrementModuleLoadCountEx() Unknown + ntdll.dll!LdrUnloadDll() Unknown + KernelBase.dll!FreeLibrary() Unknown + combase.dll!FreeLibraryWithLogging(LoadOrFreeWhy why, HINSTANCE__ * hMod, const wchar_t * pswzOptionalFileName) Line 193 C++ + combase.dll!CClassCache::CDllPathEntry::CFinishObject::Finish() Line 3311 C++ + combase.dll!CClassCache::CFinishComposite::Finish() Line 3421 C++ + combase.dll!CClassCache::CleanUpDllsForProcess() Line 7009 C++ + [Inline Frame] combase.dll!CCCleanUpDllsForProcess() Line 8773 C++ + combase.dll!ProcessUninitialize() Line 2243 C++ + combase.dll!DecrementProcessInitializeCount() Line 993 C++ + combase.dll!wCoUninitialize(COleTls & Tls, int fHostThread) Line 4126 C++ + combase.dll!CoUninitialize() Line 3945 C++ + */ + /* WIN_CoUninitialize(); */ } static SDL_bool