destructors for statically declared objects

J

joe martin

Does anyone know when or if destructors for statically declared
objects are called? I am just wondering if I am leaking system
resources each time i exit my program.

Thanks,
-Joe
 
A

Andy Sawyer

Does anyone know when or if destructors for statically declared
objects are called? I am just wondering if I am leaking system
resources each time i exit my program.

It depends on how you exit the program. If you call abort, then "no" is
the answer. If you either call 'exit', or fall off the end of 'main'
(which eventuall calls 'exit'), then the answer is "yes". If you
terminate in any other fashion, then you're on your own...:)

3.6.3p1 says:

,----
| 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.
`----

Regards,
Andy S
 
F

Francis Glassborow

joe martin said:
Does anyone know when or if destructors for statically declared
objects are called? I am just wondering if I am leaking system
resources each time i exit my program.

They are called in the same way as atexit() registered functions are
called and interleaved with those under the same rule (reverse of the
order of initialisation/registration)
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top