在C ++中退出进程的不同方式 [英] Different ways of exiting a process in C++

查看:122
本文介绍了在C ++中退出进程的不同方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

退出进程有各种方法:



例如:ExitProcess,ExitThread(从主线程),exit,abort,return from main,terminate。 / p>

我想知道每个方法对静态/全局/自动对象破坏的影响。



例如,我有一个项目崩溃(可能是由于一些释放错误)当ExitProcess被调用时,而不是当exit()被调用时。 (相关此问题,顺便说一下)。



所以基本上我想知道在什么情况下发生上述对象的释放,以什么顺序(对于VC ++)。


简单来说:唯一完全安全的做法是允许 main()或者你的线程函数,到 return



C ++标准保证,18.3),如果 exit()被调用,则将调用全局对象(包括静态对象)的析构函数,但是它明确地声明了 local 在这种情况下,不会调用变量。 exit()将调用任何用 atexit()注册的函数,并且还将刷新并关闭任何打开的stdio流(至少包括 stdin stdout stderr )。



调用 abort()保证不调用本地或全局析构函数。也不会调用 atexit()注册的函数或刷新stdio流。



调用任何Win32原语, code> ExitProcess()或 ExitThread()肯定不会调用局部变量的析构函数,并且几乎肯定不会调用任何析构函数全局对象或使用 atexit()注册的任何函数。 不建议在C ++程序中直接调用这些函数 - 基本上,这些Win32函数和C ++运行时库对彼此了解甚少。实际上,即使 MSDN文档 ExitThread() 建议C ++程序从线程函数返回,而不是调用 ExitThread()



(理论上可能的是,运行时库特别安排 ExitProcess()调用全局对象析构函数 - 这可以通过加载一个特定的DLL来完成它的入口点函数将执行这些调用,因为 ExitProcess()将调用每个加载的DLL的入口点函数 DLL_PROCESS_DETACH - 但据我所知,没有实现这一点。)


There are various ways of exiting a process:

e.g.: ExitProcess, ExitThread (from the main thread), exit, abort, return from main, terminate.

I'd like to know the effects each method has on static/global/automatic object destruction.

For example, I have a project that crashes (probably due to some deallocation error) when ExitProcess is called, but not when exit() is called. (related to this question, incidentally).

So basically I'd like to know under which circumstances deallocation of the above objects occurs, and in what order (For VC++).

解决方案

In short: The only totally safe thing to do is to allow main(), or your thread function, to return.

The C++ standard guarantees (3.6.3/1, 18.3) that destructors for global objects (including static objects) will be called if exit() is called, however it explicitly states that destructors for local variables will not be called in this case. exit() will call any functions registered with atexit(), and will also flush and then close any open stdio streams (including at least stdin, stdout, stderr).

Calling abort() is guaranteed not to call local or global destructors. Nor will it call functions registered with atexit() or flush stdio streams.

Calling any Win32 primitive such as ExitProcess() or ExitThread() will certainly not call destructors for local variables, and will almost certainly not call any destructors for global objects, or any functions registered with atexit(). Calling these functions directly in a C++ program is not advised -- basically, these Win32 functions and the C++ runtime library know nothing about each other. In fact, even the MSDN documentation for ExitThread() advises that C++ programs should return from the thread function instead of calling ExitThread().

(It is theoretically possible that the runtime library has specially arranged for ExitProcess() to call global object destructors -- this could be done by always loading a specific DLL whose entry point function will perform these calls, since ExitProcess() will call the entry point function for each loaded DLL with DLL_PROCESS_DETACH -- however to my knowledge, no implementation does this.)

这篇关于在C ++中退出进程的不同方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆