在主程序中,我dlopen和dlclose(LoadLibrary以及FreeLibrary分别)一个共享库.共享库包含一个静态变量,该变量在实例化dlopen和销毁时被实例化dlclose.这种行为在MSVC 2008和2013,GCC 3.4.6和Sunstudio 12.1上是一致的.但是,使用GCC 4.9.1和GCC 5.2.1,析构函数不再被调用dlclose.相反,它在程序退出之前被调用.
静态变量类的特殊性在于,在构造函数中,调用模板化函数get(全局范围)返回本地静态变量.
我能够使用链接到共享库的以下一个cpp文件重现此行为:
#include <iostream>
template <typename T> // In my actual code, i is of type T, however, this has no effect
int get()
{
static int i = 0;
return i;
}
class Dictionary {
public:
Dictionary()
{
std::cout << "Calling Constructor" << std::endl;
get<int>();
}
~Dictionary(){
std::cout << "Calling Destructor" << std::endl;
}
private: …Run Code Online (Sandbox Code Playgroud)