我有一个 C++ 接口(interface),并且该接口(interface)的派生类位于一个 DLL 中,我在另一个进程中使用该类,方法是包含接口(interface)头文件,并导入返回派生类对象的工厂函数 (COM风格):
MyInterface
{
public:
virtual ~MyInterface(){}
virtual A() = 0;
}
MyDerivedClass : MyInterface
{
public:
virtual ~MyDerivedClass (){...};
A(){...};
}
__declspec(dllexport) MyInterface* getObject()
{
return (new MyDerivedClass());
}
当我在 DLL 中进行测试(单元测试)时,我确实喜欢这样:
std::tr1::shared_ptr<MyInterface> MyInterfaceObj; //global
func1() //initilize
{
std::tr1::shared_ptr<MyInterface> temp(getObject());
MyInterfaceObj.swap(temp);
}
func2()
{
//use MyInterfaceObj;
}
一切正常,我使用视觉泄漏检测器,没有提示,并且我可以看到调用了 MyDerivedClass 析构函数。
但是,当我在进程中执行完全相同的操作(加载 DLL)时,MyDerivedClass 析构函数永远不会被调用,并且 VLD 会提示内存泄漏。
但是,如果我在 func2() [在我的进程中]声明所有内容,则一切正常,没有泄漏,并且析构函数被调用:
func2()
{
std::tr1::shared_ptr<MyInterface> MyInterfaceObj; // not global anymore
std::tr1::shared_ptr<MyInterface> temp(getObject());
MyInterfaceObj.swap(temp); //I know this is useless here, just wanted to have the same steps as before
//use MyInterfaceObj;
}
我需要在我的进程中拥有第一个结构(一个全局变量,由一个函数初始化,然后在心跳函数中使用)。
知道为什么会发生这种情况吗?!,我尝试创建一个函数来释放内存(它具有“删除此”)并将其传递给智能指针构造函数,但它不会改变任何内容。
(使用 Visual C++ 2008 sp1)
最佳答案
不保证全局变量的销毁顺序。对于 DLL 中的测试代码,它显然会在泄漏检测发生之前破坏全局,但当您将其放在应用程序中时,它不会破坏全局。在应用程序退出之前它仍然会被销毁。
当变量位于函数内部而不是全局变量时,它只会存在于函数范围内。它将在函数结束时被销毁。
关于c++ - 全局智能指针未正确清理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16007223/