Are destructors run when calling exit()?

23,716

Solution 1

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.

Solution 2

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.

Share:
23,716
WilliamKF
Author by

WilliamKF

Updated on March 19, 2020

Comments