如何知道函数何时从“atexit”执行? [英] How to know when a function is executed from "atexit"?

查看:73
本文介绍了如何知道函数何时从“atexit”执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个全局对象(我知道它很糟糕)但是当它被销毁时,析构函数是从 atexit mecanism调用的。

我的问题是:这个析构函数调用了一些函数,其中一个似乎不允许从这一点调用(在我的例子中它是 fopen 函数)。

如果我知道这个代码是从 atexit 调用的,我将避免在运行时调用该函数。



任何想法?



事实上,问题有点困难:

- 全局对象在DLL中(与程序隐含地联系起来)

- 在这个全局对象的生命周期中,它可能会加载其他一些dll(动态)

- 在某些情况下,它可以释放动态加载的应用程序退出前的dll(没问题)

- 在其他情况下,清理由全局对象的析构函数完成。



我的问题在于其中一个动态加载的DLL。



如果我在动态加载的DLL中添加atexit函数,它将在卸载DLL时被调用(我猜)但是在程序结束之前dll没有卸载而没有调用FreeLibrary。 br />
如果我在全局对象dll中添加atexit函数,它将无法访问动态加载的dll的标志。

解决方案

全球是邪恶的。在你的情况下,你可以用更邪恶来对抗邪恶。

假设你的代码是这样的:



  #include   <   iostream  >  
#include < cstdlib >

使用 命名空间性病;

X myGlobalX;
// 程序使用FinalFunction调用atexit
// FinalFunction在程序结束前执行
void FinalFunction( void
{

// myGlobalX。~X()在此处调用。
}
int main()
{
atexit(FinalFunction);
}



(来源: http:// www .cprogramming.com / fod / atexit.html [ ^ ])。



您可以添加一个全局布尔标志,如下所示:



< pre lang =c ++> #include < < span class =code-leadattribute> iostream >
#include < cstdlib >

使用 namespace std;

X myGlobalX;
bool calledFromFinalFunction = false ; // 首次更改
// 程序使用FinalFunction调用atexit
// FinalFunction在程序结束前执行
void FinalFunction( void
{
calledFromFinalFunction = true ; // 第二次更改
// myGlobalX。~X()在这里调用; 它可以查看 calledFromFinalFunction 并相应地采取行动。
}
int main()
{
atexit(FinalFunction);
}





我甚至羞于暗示这一点,但无论如何,希望这会有所帮助。


我通过在第一个dll中添加一个新的导出函数来绕过这个问题,该函数必须由程序调用才能执行清理。



它将导致将全局对象更改为通过第一次调用dll初始化并由显式清理函数销毁的对象。


I have a global object (I know it is bad) but when it is destroyed, the destructor is called from the atexit mecanism.
My problem is : this destructor calls some functions and one of them seems to not allow been called from this point (in my case it is the fopen function).
If I known this code is called from atexit, I will avoid calling that function on runtime.

Any idea ?

In fact, the problem is a little more difficult:
- The global object is inside a DLL (linked implicitely with the program)
- in the life of this global object, it may load some other dlls (dynamically)
- in some conditions, it frees the dynamically loaded dlls before application exit (no problem)
- in other condition, the "cleanup" is done by the destructor of the global object.

And my problem is in one of the dynamically loaded DLLs.

if I add atexit function in the dynamically loaded DLL, it will be called when DLL is unloaded (I guess) but the dll is not unloading before the program ends without a call to FreeLibrary.
if I add the atexit function in the global object dll, it will not give access to the flag to the dynamically loaded dlls.

解决方案

Globals are evil. In your case, you can fight evil with even more evil.
Let's say your code is someting like this:

#include <iostream>
#include <cstdlib>

using namespace std;

X myGlobalX;
//Program calls atexit with FinalFunction
//FinalFunction is executed before program ends
void FinalFunction(void)
{
 
// myGlobalX.~X() is called here. 
}
int main()
{
  atexit(FinalFunction);
}


(source: http://www.cprogramming.com/fod/atexit.html[^]).

You can add a global boolean flag, like this:

#include <iostream>
#include <cstdlib>

using namespace std;

X myGlobalX;
bool calledFromFinalFunction = false; // first change
//Program calls atexit with FinalFunction
//FinalFunction is executed before program ends
void FinalFunction(void)
{
   calledFromFinalFunction = true; // second change
// myGlobalX.~X() is called here; it can look at calledFromFinalFunction and act accordingly.
}
int main()
{
  atexit(FinalFunction);
}



I'm ashamed to even suggest this, but anyway, hope this helps.


I bypassed the problem by adding a new exported function in the first dll that must be called by the program to perform the cleanup.

It will lead to change the global object to an object initialized by a first call to the dll and destroyed by the explicit cleanup function.


这篇关于如何知道函数何时从“atexit”执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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