https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101933
Bug ID: 101933 Summary: Unloaded dll with global std::mutex causes exe to crash on exit #38 Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: mailnew4ster at gmail dot com Target Milestone: --- Reproduction on Windows: Create and compile dll.cc as following: ``` #include <mutex> std::mutex x; ``` g++.exe dll.cc -shared -o test.dll Create and compile exe.cc as following: ``` #include <windows.h> int main() { auto x = LoadLibrary("test.dll"); MessageBox(0,0,0,0); FreeLibrary(x); MessageBox(0,0,0,0); return 0; } ``` g++.exe exe.cc -o test.exe Run test.exe and observe a crash after seeing both message boxes. The crash happens in msvcrt.doexit, which tries to call a cleanup function inside the dll which was already freed. I'm using TDM-GCC, and I also reported the issue there: https://github.com/jmeubank/tdm-gcc/issues/38 I'm not sure whether it's specific to TDM-GCC, it applies some patches on GCC, but they are very small and I think they probably don't affect this issue.