MSVC std::atexit not working as expected

Hi,

I had a rather nasty surprise yesterday, when I learned that Microsoft's implementation of the "std::atexit" function is a little ... misleading.

The C++ standard says that all statics constructed before a call to "std::atexit" will still be alive when the given function is called. However, apparently Microsoft thinks that shouldn't apply to statics constructed within DLLs. At the moment, all static variables not within the base library for my application are being destroyed BEFORE the function given to "std::atexit" is called, even though "std::atexit" is most definitely called after they are constructed (it's done in a function called "Application::Initialize", which is the first function called in "main". It registers "Application::AtExit" to be called at exit).

The only solution I've found to fix this is to directly call "std::atexit(Application::AtExit)" in "main", but that's not what I want. I also find this very misldeading that different behavior occurs based on where "std::atexit" is called, rather than when.

Needless to say, I've found this behavior to be very frustrating (compounded with the fact that Linux's implementation of dynamic libraries DOES work as expected). Is there any Microsoft specific alternative to "std::atexit" that calls the function, you know, AT THE ACTUAL EXIT OF THE PROGRAM? I've found "_onexit" works exactly the same as "std::atexit", and I'd rather not resort to implementing "dllmain", since that would be very difficult to abstract between platforms.

Thanks

Edit:
After rereading my post I noticed I made it clear what I don't want, but not really what I do want.

Basically, I want to be able to execute a function immediately after 'main' returns. I need it to execute before any static variables are destroyed, and before any DLL's are unloaded. It should almost work as if the function was called at the end of 'main', only that all variables in 'main' will have gone out of scope (this is crucial, due to the memory management technique I'm using).

It would seem that "std::atexit" is the ideal vehicle for this behavior, but Microsoft's implementation is simply incompatible with what I'm trying to do (unlike Linux).
Last edited on
Topic archived. No new replies allowed.