PierreChatelier-6741 avatar image
0 Votes"
PierreChatelier-6741 asked PierreChatelier-6741 commented

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

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 :

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I confirm that removing the /ENTRY defined to "main" in the original project settings and letting it blank (it will be routed to my int main(cli::array<System::String^>^) anyway) fixes the crash.
The question is still : how can it work in the first project ?

0 Votes 0 ·
JeanineZhang-MSFT avatar image JeanineZhang-MSFT PierreChatelier-6741 ·


Could you please describe your problem in more detail? According to the demo, whether you are creating C++ Windows Forms application? If so I suggest you could refer to the link:

“how can it work in the first project?” Which project are you referring to? Could you please provide the key code snippets of these two projects separately?

0 Votes 0 ·

I can't publish the initial project I am talking about in my post. And extracting a snippet is exactly what I have done by createing a "minimal" project based on it.
But let's close that bug.
The fix is about /ENTRY and main, whatever the reason. Let's not spend hours trying to figure out why. I thought there was an bvious answer, if there is not, never mind.

I am really sad that C++/CLI is disliked by Microsoft. It is far better than C# for serious development of a GUI application with tons of dependencies to C++ libraries.

0 Votes 0 ·

0 Answers