Application I'm working on is crashing on close. The application includes C++ native code loaded from dynamic libraries.
The native code starts cleanup through the GC Finalizer Thread, when it's main container (which is a CLR class) is deleted by GC.
After digging a lot, I found that this is purely time related - if I add Sleep(10000)
to one of the native cleanup functions that are being ran when application is closing, it will crash. This essentially happens because the entire native part has a check that basically looks like this:
// native library destruction
~MyEntireDll()
{
if(!EverythingProperlyCleanedUp())
{
log("Killed without proper cleanup.");
CrashOnPurpose();
}
}
Colleague suggested that this is because when you press the close button in WPF, it will signal the threads to end, and if they do not do it in set amount of time, it kills them the hard way. This means when our native part of the program takes long to clean up, it is killed - but upon being killed like this, destructors of global objects are still called.
We don't want it to be killed without proper cleanup anyway - the fact that we found out thanks to the intentional crash above is a good thing.
My question is, where do I set how long to wait before killing background threads? Can I disable this feature and have my own way of dealing with hangs? I didn't find anything in the documentation, in fact I found no mention of the timeout at all.