Possible Duplicate:
Will exit() or an exception prevent an end-of-scope destructor from being called?
In C++, when the application calls exit(3) are the destructors on the stack supposed to be run to unwind the stack?
Possible Duplicate:
Will exit() or an exception prevent an end-of-scope destructor from being called?
In C++, when the application calls exit(3) are the destructors on the stack supposed to be run to unwind the stack?
No, most destructors are not run on exit().
C++98 §18.3/8 discusses this.
Essentially, when exit is called static objects are destroyed, atexit handlers are executed, open C streams are flushed and closed, and files created by tmpfile are removed. Local automatic objects are not destroyed. I.e., no stack unwinding.
Calling abort lets even less happen: no cleanup whatsoever.
If your OS is reasonable (Unix, Linux, or a recent Windows), calling exit() tells the kernel to de-allocate all the processes' memory. The stack doesn't need to be unwound; it will simply cease to exist.
throwa special type instead of callingexit, catch it inmain, and thenreturnfrommaininstead ofexitfrommain?exit()? In one of my apps, I do throw ancustomExitobject and catch it in main and use an integer value from thecustomExitobject to supply the return value from main, this way my RAII destructors are run to clean up things like temporary files.throwapproach to ensure everything is destructed in a normal program exit