Are destructors run when calling exit()? [duplicate]

前提是你 提交于 2019-11-27 14:25:42

问题


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?


回答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.




回答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.



来源:https://stackoverflow.com/questions/7054685/are-destructors-run-when-calling-exit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!