我们应该在 C++ 中使用 exit 吗?
should we use exit in C++?
根据 C++ 参考
exit
terminates the process normally, performing the regular cleanup
for terminating programs.
Normal program termination performs the following (in the same order):
Objects associated with the current thread with thread storage
duration are destroyed (C++11 only). Objects with static storage
duration are destroyed (C++) and functions registered with atexit are
called. All C streams (open with functions in ) are closed
(and flushed, if buffered), and all files created with tmpfile are
removed. Control is returned to the host environment.
Note that objects with automatic storage are not destroyed by calling
exit (C++).
据我所知,当进程终止时,进程使用的所有存储都被回收,那么自动存储的对象不被销毁有什么影响?
如果不调用这些析构函数,它们的副作用就不会发生:释放其他进程中的资源、删除该文件夹外的临时文件、刷新非 C 流文件等等等等
问题在于,并非所有您可能以某种方式获得所有权的东西都列在标准中作为清理对象。
例如,许多程序和库会执行诸如创建锁定文件、启动后台进程、更改系统设置等操作。如果您调用 exit
,这些 可能 会被 OS 清除,但并非必须如此。未能释放这些可能会产生从无法重新启动程序(典型的锁定文件)到整个系统故障(不太可能,但在某些情况下可能)的影响。
真实的,切题的轶事。 我曾经为我的项目使用 OIS,一个输入库。每次我在调试器中终止我的程序时,系统范围内的键重复都会被破坏,因为 OIS 'temporarily' 在 Linux 中禁用了它。我通过更改设置(然后完全转储 OIS)修复了它,但这很好地说明了在您自己清理环境之前您可能 运行 调用 exit
的问题。
在 C++ 中,您最好使用 std::terminate
rather than exit
or abort
to do an orderly fatal error exit, because code that you're using may have installed a terminate handler 进行关键清理。
默认 std::terminate
处理程序调用 abort
。
堆栈未展开。
回复
” what's the impact that objects with automatic storage are not destroyed?
由于未调用析构函数,因此未执行它们可能执行的清理。例如,在 Windows 中,如果控制台 window 具有自定义文本缓冲区,则它可能无法使用。临时文件可能留在磁盘上。辅助进程可能不会关闭。等等。
如果尝试进行一般清理,则必须权衡发生不良事件的可能性,例如因为断言已触发,表明关于进程状态的一些基本假设不成立。
根据 C++ 参考
exit
terminates the process normally, performing the regular cleanup for terminating programs.Normal program termination performs the following (in the same order): Objects associated with the current thread with thread storage duration are destroyed (C++11 only). Objects with static storage duration are destroyed (C++) and functions registered with atexit are called. All C streams (open with functions in ) are closed (and flushed, if buffered), and all files created with tmpfile are removed. Control is returned to the host environment.
Note that objects with automatic storage are not destroyed by calling exit (C++).
据我所知,当进程终止时,进程使用的所有存储都被回收,那么自动存储的对象不被销毁有什么影响?
如果不调用这些析构函数,它们的副作用就不会发生:释放其他进程中的资源、删除该文件夹外的临时文件、刷新非 C 流文件等等等等
问题在于,并非所有您可能以某种方式获得所有权的东西都列在标准中作为清理对象。
例如,许多程序和库会执行诸如创建锁定文件、启动后台进程、更改系统设置等操作。如果您调用 exit
,这些 可能 会被 OS 清除,但并非必须如此。未能释放这些可能会产生从无法重新启动程序(典型的锁定文件)到整个系统故障(不太可能,但在某些情况下可能)的影响。
真实的,切题的轶事。 我曾经为我的项目使用 OIS,一个输入库。每次我在调试器中终止我的程序时,系统范围内的键重复都会被破坏,因为 OIS 'temporarily' 在 Linux 中禁用了它。我通过更改设置(然后完全转储 OIS)修复了它,但这很好地说明了在您自己清理环境之前您可能 运行 调用 exit
的问题。
在 C++ 中,您最好使用 std::terminate
rather than exit
or abort
to do an orderly fatal error exit, because code that you're using may have installed a terminate handler 进行关键清理。
默认 std::terminate
处理程序调用 abort
。
堆栈未展开。
回复
” what's the impact that objects with automatic storage are not destroyed?
由于未调用析构函数,因此未执行它们可能执行的清理。例如,在 Windows 中,如果控制台 window 具有自定义文本缓冲区,则它可能无法使用。临时文件可能留在磁盘上。辅助进程可能不会关闭。等等。
如果尝试进行一般清理,则必须权衡发生不良事件的可能性,例如因为断言已触发,表明关于进程状态的一些基本假设不成立。