首页 > 解决方案 > dlclose 不释放库句柄以防访问静态类成员,例如调用 boost::this_fiber::get_id()

问题描述

我有一个场景如下,无法关闭图书馆

liba.so

  #include <iostream>
  #include <boost/fiber/all.hpp>
  using namespace std;

  int func1(int)
  {
    cout << "this fiber id " << boost::this_fiber::get_id() << endl; // if this line is commented out then the problem resolved.
    cout << "It is a func1" << endl;
    return 1;
  }

我从指定了 -fno-unique-symbol 的 cpp 文件中创建了一个库,这样它就不会保留符号。

主要代码

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <sys/types.h>
using namespace std;
int fun(string & flName)  {
  void *handle;
  typedef int (*fn)(int);
  fn fn1;
  handle = dlopen(flName.c_str(), RTLD_LOCAL|RTLD_LAZY);
  fn1 = (fn)dlsym(handle, "_Z5func1I");
  (*fn1)(2);
  cout << "handle :" << handle << endl;
  int ext_code = dlclose(handle);
  printf ("end exit code %d\n", ext_code);
}

int main(void)    {
  std::string fileName("dirpath/liba.so");
   fun(fileName);
   fun(fileName);
   fun(fileName);
   return 0;
}

输出,当 this_fiber 被注释掉时

----- 但是当 this_fiber::get_id() 处于活动状态时

有人可以解释阻止库句柄的原因并告诉我如何调整代码以便我可以关闭库

标签: c++cshared-librariesboost-fiber

解决方案


推荐阅读