Sign in to follow this  
IFooBar

What happens first? static deinitialization or atexit?

Recommended Posts

I know that anything you register with atexit is called after the main exits and inside some function called doexit (msvc). I'm assuming that static destructors are called over here too because I cant seem to find any other "cleanup" places. So assuming that my assumption is correct, am I correct in assuming that there is no way to know whether a function you registered with atexit inside some static constructor will be called before or after the static destrocutor? Or are static destructors specifically registered before anything else happens?

Share this post


Link to post
Share on other sites
C++ Standard Section 3.6.3, Paragraph 3:
Quote:

If atexit is called during the construction of an object [with static storage duration], the complete object to which it belongs shall be destryoed before the registered function is called.

Share this post


Link to post
Share on other sites
Muchos gracias.

I think I'm going to have to get me one of those standard specifications. I tried looking around for it, it's not a download right? I probably have to get it off of ansi.org or iso.org??

One other question. Does it also say if static destructors are called before or after functions registered with atexit (regardless of whether they were registered inside s static constructor or not)

Thanks again.

Share this post


Link to post
Share on other sites
Quote:
Original post by IFooBar
I think I'm going to have to get me one of those standard specifications. I tried looking around for it, it's not a download right? I probably have to get it off of ansi.org or iso.org??

There's a "C++ Standard Draft" available for download somewhere - I hear it's close to the final standard, but I don't really know. The standard itself can be bought as an ebook/pdf (at ~$10) or a hard-copy. It's not free.

Share this post


Link to post
Share on other sites
Right now the cheapest way to get a hold of a complete, legal copy of the C++ Standard is to grab the hard cover from Wiley books, available from the usual suspects, including amazon (if you want to support gamedev via the referer ID). This is the latest version, ISO/IEC 14882:2003.

The PDF versions can actually be a lot more expensive. ANSI, for example, sells it for $281.00 US (though you can get a member discount). Other national standards bodies may give you a better price depending on the current exchange rates, but last time I checked, no one (that actually had legal publishing rights to the document) was selling it at less than the equivalent of $100 US.

Draft versions of the standard can be found all over the web. For example here. The gist is the same, but the language lawyering tidbits can be different.

Anyway, here's the full text of 3.6.3 paragraph 3 (From ISO/IEC 14882:1998, I don't have 14882:2003 handy right now):
Quote:

If a function is registered with atexit (18.3) then following the call to exit, any objects with static storage duration initialized prior to the registration of that function shall not be destroyed until the registered function is called from the termination process and has completed. For an object with static storage duration constructed after a function is registered with atexit, then following the call to exit, the registered function is not called until the execution of the object's destructor has completed.


If you compare to the draft standard I linked above, you'll see that a "shall" replaced a "will", so not much a difference there.

Share this post


Link to post
Share on other sites
sweet! thanks - especially for the link to that draft. Amazing how in almost every section there's something new to learn [totally].

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this