--- cygming-crtbegin.c.bak 2014-07-03 16:41:43.116600000 +0100 +++ cygming-crtbegin.c 2014-07-03 18:39:07.436800000 +0100 @@ -99,8 +99,21 @@ if (h) { - /* Increasing the load-count of LIBGCC_SONAME DLL. */ - hmod_libgcc = LoadLibrary (LIBGCC_SONAME); + /* + Increase the load-count of the LIBGCC_SONAME DLL by one, so that it remains + loaded as long as this module is, so that __gcc_deregister_frame() will call + __deregister_frame_info() in that DLL, to match the register_frame_info() + we do now. + + Unless this *is* LIBGCC_SONAME DLL, where increasing our own load-count would + make us self-pinning. + */ + HMODULE hmod_this = 0; + GetModuleHandleEx (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + (LPCTSTR)__gcc_register_frame, &hmod_this); + if (hmod_this != h) + hmod_libgcc = LoadLibrary (LIBGCC_SONAME); + register_frame_fn = (void (*) (const void *, struct object *)) GetProcAddress (h, "__register_frame_info"); }