atexit and global destructors

S

Serve Laurijssen

Does the C++ standard say whether destructors are called before or after
"atexit" functions are called.
Or is nothing said about that?
 
J

Jacques Labuschagne

Serve said:
Does the C++ standard say whether destructors are called before or after
"atexit" functions are called.
Or is nothing said about that?

3.6.3/3 says
"If a function is registered with atexit 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
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."

Jacques.
 
S

Serve Laurijssen

Jacques Labuschagne said:
3.6.3/3 says
"If a function is registered with atexit 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.

So if I understand correctly,
when I call atexit at the first line in main, all global static objects will
be destroyed after the registered function is executed?
 
T

tom_usenet

So if I understand correctly,
when I call atexit at the first line in main, all global static objects will
be destroyed after the registered function is executed?

No, since not all global static objects are guaranteed to have been
initialized before the first line of main (although they will on most
platforms when not using dynamic loading). The standard allows global
statics in to remain uninitialized until you use the translation unit
that the static is initialized in, to facilitate dynamic loading of
modules.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

atexit problems and questions.... 9
atexit override in your own module 1
Some questions about atexit 2
atexit 19
atexit() and DLLs 1
std::atexit 3
Inline destructors and construtors 8
atexit handler in IDLE? 0

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top