Have an EXE which implicitly loads some DLL’s and others explicitly (LoadLibrary). This EXE is executing its ExitProcess (1 thread in process left) and as part of that busy unloading a DLL, say A.DLL.
A.DLL (unfortunately) has a static. The atexit callback is called and this static’s destructors begins to be called, leaving a trail of destructors, until a destructor decides to do some cleanup and loads a DLL to do this. This DLL performs some methods until a crash occurs due to memory access violation since a static it was about to use in this DLL is no longer present. Looking at the stack trace of this static, its destructor has been called already as part of its DLL unload.
What happened? The DLL was loaded, performed some methods, goes to use a static (in the same DLL) but this has been destructed (static only gets destructed when dll is unloaded). So is it in a half limbo state of executing methods but is also being destructed?
The EXE seems in the context of __tmainCRTStartup which implies the user created main has returned? Are DLL’s unloaded while in the context of the user main or tmainCRTStartup?
It’s this simple: Destructors of static objects are called in reverse order of their creation, this is internally done by registering atexit callbacks. The only case where this differs is if you manually unload (FreeLibrary) a DLL.
The problem you describe only shows that you have a cyclic dependency, which can easily happen with static constructors/destructors. You should be careful in what you do in destructors, especially loading a DLL at this point seems quite dangerous to me.