last function to be called

N

Nick Keighley

Hi,

I want a particular function to run after everything has beeen
destructed.
It's looking for memory leaks.

atexit() doesn't seem to suit my purpose as global statics can be
destructed
after all the atexit()s.

Would a global static at the beginning of the file containing main()
do the job
or have I run into another aspect of the "static initialization order
fiasco"?
 
A

Andre Kostur

Hi,

I want a particular function to run after everything has beeen
destructed.
It's looking for memory leaks.

atexit() doesn't seem to suit my purpose as global statics can be
destructed
after all the atexit()s.

Would a global static at the beginning of the file containing main()
do the job
or have I run into another aspect of the "static initialization order
fiasco"?

Compiler-dependant. A global static at the beginning of the translation
unit containing main() will only be known to have been constructed before
main, but not necessarily before global objects in other translation units.
 
N

Nick Keighley

Compiler-dependant. A global static at the beginning of the translation
unit containing main() will only be known to have been constructed before
main, but not necessarily before global objects in other translation units.


so the question is not, portably, answerable? There is no easy way to
generate
a last function or DTOR?
 
A

Alan Johnson

Nick said:
so the question is not, portably, answerable? There is no easy way to
generate
a last function or DTOR?

Perhaps too intrusive/fragile for your needs, but you could modify every
translation unit to have a global variable of some type T as the first
thing in that translation unit. T could have a static reference
counter, and when ~T() is executed with reference count 1, that should
be the last thing to execute.
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top