相关疑难解决方法(0)

dlclose()不会调用全局对象的析构函数

plugin1.cpp:

#include <iostream>

static class TestStatic {
public:
  TestStatic() {
     std::cout << "TestStatic create" << std::endl;
  }
  ~TestStatic() {
     std::cout << "TestStatic destroy" << std::endl;
  }
} test_static;
Run Code Online (Sandbox Code Playgroud)

host.cpp

#include <dlfcn.h>
#include <iostream>
int main(int argc,char *argv[]) {
   void* handle = dlopen("./plugin1.so",RTLD_NOW | RTLD_LOCAL );
   dlclose(handle);
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

构建和运行:

>g++ -c plugin1.cpp -o plugin1.o -fPIC
>g++ -shared plugin.o -o plugin1.so
>g++ host.cpp -o host -ldl
>./host
>TestStatic create
>Segmentation fault
Run Code Online (Sandbox Code Playgroud)

为什么TestStatic :: ~TestStatic在'exit()'处调用但不在'dlclose()'处调用?

c++ dll gcc atexit dlopen

22
推荐指数
1
解决办法
4595
查看次数

标签 统计

atexit ×1

c++ ×1

dll ×1

dlopen ×1

gcc ×1