Jump to content
  • Advertisement
Sign in to follow this  

What's that function called that is called just before the program exits?

This topic is 4956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I read somewhere (I believe on this forum somewhere) about a function that is called just before a C/C++ program exits. This function is supposedly overridable to do some custom stuff just before the program exits. I'd like to use this for some memory debugging to make sure my singleton instances do free all memory as they are supposed to. I can't find the name of this function. Does anyone know?

Share this post


Link to post
Share on other sites
Advertisement
sounds like your talking about the standard library function atexit, you can't override the function but give a it the address of a function to be called "at exit" time, note you can give more than one function to be called, which happens in last-in, first-out order.

Share this post


Link to post
Share on other sites
Doesn't work if the program ends abnormally (for example via a signal, unhandled exception, or an explicit call to exit() or abort()).

Share this post


Link to post
Share on other sites
Quote:
Original post by Sharlin
Doesn't work if the program ends abnormally (for example via a signal, unhandled exception, or an explicit call to exit() or abort()).


besides abnormal exiting, things get destroyed outside the scope of main, also he might not be using main.

Share this post


Link to post
Share on other sites
If you're using the GNU C Compiler, check out __attribute__((constructor)) and __attribute__((destructor)).

Just remember, you have no guarantees for what order initialization or deinitialization functions are called. If things depend on each other being initialized in a certain order, you will need to do that explicitly from a single init-level function (aka, if init_lib_a() needs to be called before init_lib_b(), and fini_lib_b() needs to be called before fini_lib_b(), you'd do something like this:)

std::list<std::string> my_log;

__attribute__((constructor)) void my_init( void ) //will get called before main starts
{
init_lib_a();
init_lib_b();
my_log.push_back("Message"); //BAD IDEA: this might be, but might not be, initialized yet!!!
}

__attribute__((destructor)) void my_fini( void ) //will get called after main exits
{
fini_lib_b();
fini_lib_a();
my_log.front(); //BAD IDEA: this might have allready been destroyed - we have no clue really.
}

__attribute__((destrcutor)) void my_other_fini( void ) //will get called after main exits - and either before or after my_fini.
{
//...
}


Also be aware that these functions are competing toe to toe with C++ ctors/dtors... don't depend on any C++ globals that need initialization or deinitialization.

Share this post


Link to post
Share on other sites
Quote:
Original post by snk_kid
sounds like your talking about the standard library function atexit, you can't override the function but give a it the address of a function to be called "at exit" time, note you can give more than one function to be called, which happens in last-in, first-out order.


Yes! That is the one I was talking about!

Quote:
Original post by snk_kid
Quote:
Original post by Sharlin
Doesn't work if the program ends abnormally (for example via a signal, unhandled exception, or an explicit call to exit() or abort()).


besides abnormal exiting, things get destroyed outside the scope of main

This is indeed the case with a number of classes with core functionality and why I need to write my memory map at the very last moment.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!