我有一个foo类的私有静态成员变量(std::vector<double> values
)。有一个foo封装的对象(接口类和实现类成语)。这是头文件“foo.h中”:dll中静态成员变量的生存期
class foo
{
public:
foo();
~foo();
private:
static std::vector<double> values;
};
而且定义文件“Foo.cpp中”:
#include "foo.h"
std::vector<double> foo::values;
foo::foo()
{
values.resize(10UL);
}
foo::~foo()
{
for (auto& v :values)
{
v = 99.9;
}
}
富是DLL,在那里我有我的接口类(文件内部构造'interface.h'):
class interface
{
public:
interface();
~interface();
foo myFoo;
};
extern "C" foo* getFooObject();
和getter函数在我的 'interface.cpp' 文件:
#include "interface.h"
interface::interface(){}
interface::~interfacet(){}
interface myInterface;
extern "C"
{
foo* getFooObject()
{
return &myInterface.myFoo;
}
}
在我的主程序中,我使用::LoadLibrary(libraryName)
(Windows操作系统)加载dll。当我执行::FreeLibrary(libraryHandle)
并在析构函数中设置断点时~foo()
variables
似乎已经被破坏。有人可以帮助我在静态成员变量的生命期在DLL中?如何防止调用析构函数时静态成员变量已被销毁?
预先感谢您。
可能是一个类似的问题:
c++ Static variables in dynamic DLL lifetime. or why they dead?
我使用Visual Studio 2013与英特尔Parallel Studio XE 2016
你的问题没有解释'foo'是如何构建的。它是在DLL内部构建的,并通过工厂函数或getter暴露出来?或者,也许有一个单一的静态实例'foo'里面的dll得到返回莫名其妙? –
你不能阻止,必须在FreeLibrary之前调用〜foo()。 – KonstantinL
@RudolfsBundulis:感谢您的评论。我编辑了我的问题并添加了界面以显示foo是如何构建和暴露的。 –