小编Ily*_*nov的帖子

C++强制卸载共享库

我正在尝试创建一个多次重新加载共享库的应用程序.但在某个时间点,dlmopen失败并出现错误

/usr/lib/libc.so.6: cannot allocate memory in static TLS block

以下是重现此问题的最小代码:

#include <dlfcn.h>
#include <cstdio>
#include <vector>

int main() {
  for (int i = 0; i < 100; ++i) {
    void *lib_so = dlmopen(LM_ID_NEWLM, "lib.so", RTLD_LAZY | RTLD_LOCAL);
    if (lib_so == NULL) {
      printf("Iteration %i loading failed: %s\n", i, dlerror());
      return 1;
    }
    dlclose(lib_so);
  }

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

并清空lib.cpp,用.编译

g++ -rdynamic -ldl -Wl,-R . -o test main.cpp
g++ -fPIC -shared lib.cpp -o lib.so
Run Code Online (Sandbox Code Playgroud)

更新

它似乎即使用一个线程也会崩溃.问题是:如何强制库卸载或销毁使用的未使用的命名空间LM_ID_NEWLM

c++ linux shared-libraries

8
推荐指数
1
解决办法
1333
查看次数

标签 统计

c++ ×1

linux ×1

shared-libraries ×1