If benjymous's answer isn't appropriate for some reason or other, std::atexit
may help:
int atexit( void (*func)() );
Registers the function pointed to by func to be called on normal program termination (via std::exit() or returning from the cpp/language/main function)