小编Inf*_*ity的帖子

不在dlclose上调用共享库中的全局静态变量的析构函数

在主程序中,我dlopendlclose(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)

c++ gcc shared-libraries dynamic-linking dlopen

7
推荐指数
1
解决办法
1638
查看次数

标签 统计

c++ ×1

dlopen ×1

dynamic-linking ×1

gcc ×1

shared-libraries ×1