Skip to content

Commit 2ecda5b

Browse files
committed
fix vanilla std::terminate bug on exit/restart
1 parent fd0fc2d commit 2ecda5b

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

loader/src/hooks/GameExit.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,27 @@ struct GameExitHook : Modify<GameExitHook, CCDirector> {
6060
#endif
6161

6262
}
63+
64+
#ifdef GEODE_IS_WINDOWS
65+
$on_game(Exiting) {
66+
// On Windows, the game creates an std::thread in CCEGLView::setupWindow and stores it in a static variable,
67+
// but never joins it or calls detach on it. This leads to a call to std::terminate during static destruction,
68+
// and that may have unexpected consequences, such as stalling the game on restart and making it terminate abnormally.
69+
//
70+
// This fix simply detaches the thread before the game has a chance to do things improperly.
71+
// For people updating this to a newer GD version: look near the end of setupWindow:
72+
//
73+
// uVar4 = _beginthreadex(...);
74+
// ...
75+
// _DAT_1801a8530 = uVar4; <-- this is the address we want!
76+
77+
#if GEODE_COMP_GD_VERSION != 22081
78+
#pragma message("Unsupported GD version!")
79+
return;
80+
#endif
81+
82+
auto addr = reinterpret_cast<void*>(geode::base::getCocos() + 0x1a8530);
83+
auto thread = reinterpret_cast<std::thread*>(addr);
84+
if (thread->joinable()) thread->detach();
85+
}
86+
#endif

0 commit comments

Comments
 (0)