minimal C++ project crash on launch (atexit/_onexit) when using static initialization... but not in original bigger project

Pierre Chatelier 51 Reputation points
2021-03-19T09:33:17.503+00:00

I have a project mixing native C++ and C++/CLI, that compiles and runs flawlessly.
I have tried to start a new project with the same structure (I just copied and renamed the files before cleaning up by removing the code), and it crashes on launch, despite it is now almost empty, without any third-party dependency.

The crash (in _onexit) seems to occur because of static initialization of a global C++ mutex variable. If I remove this variable, the crash goes away.

In the forums, I can find references to such a crash because of things like /NOENTRY, /subsystem, entry point subtilities. But apart from the crash, I just can't understand why everything works fine in the original project, and that it doesn't in a minimalist version of the project where I just removed code! The project structure/settings are identical, so all the /NOENTRY, /SUBSYSTEM and main() subtilities are out of topic since it works fine in the first place.
I tried to recompile and run the original project with additional static variables, and it still runs without any problem.

This is not a compiler problem either, since I use the latest VS 2019 16.9.2 for both.
I suspect that a library dependency of the original project could perform some magic CRT initialization that prevents crashing, but should it be the case, I cannot link to a library just "because it makes things work", I have to understand what is to be done.

Of course, I can't post the big project, here is a link to the minimal one : TestCrashStatic.zip

.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
291 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
2,619 questions
{count} votes