From: Jonathan Turkanis (technews_at_[hidden])
Date: 2005-03-08 22:15:19
Jason Hise wrote:
> Does the standard specify whether static variables are destroyed
> before or after atexit is triggered?
I've pasted in section 3.6.3 of the standard.
Destructors (12.4) for initialized objects of static storage duration (declared
at block scope or at namespace scope) are called as a result of returning from
main and as a result of calling exit (18.3). These objects are destroyed in the
reverse order of the completion of their constructor or of the completion of
their dynamic initialization. If an object is initialized statically, the object
is destroyed in the same order as if the object was dynamically initialized. For
an object of array or class type, all subobjects of that object are destroyed
before any local object with static storage duration initialized during the
construction of the subobjects is destroyed.
If a function contains a local object of static storage duration that has been
destroyed and the function is called during the destruction of an object with
static storage duration, the program has undefined behavior if the flow of
control passes through the definition of the previously destroyed local object.
If a function is registered with atexit (see <cstdlib>, 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 atexit is
called during the construction of an object, the complete object to which it
belongs shall be destroyed before the registered function is called.
Calling the function
declared in <cstdlib> terminates the program without executing destructors for
objects of automatic or static storage duration and without calling the
functions passed to atexit().
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk