dlclose()不会调用全局对象的析构函数 [英] dlclose() does not call the destructor of global objects
问题描述
plugin1.cpp:
#include< iostream>
static class TestStatic {
public:
TestStatic(){
std :: cout< TestStatic create< std :: endl;
}
〜TestStatic(){
std :: cout< TestStatic destroy< std :: endl;
}
} test_static;
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;
}
构建并运行:
> g ++ -c plugin1.cpp -o plugin1.o -fPIC
> g ++ -shared plugin.o -o plugin1.so
> g ++ host.cpp -o host -ldl
> ./ host
> TestStatic create
>分段错误
为什么TestStatic ::〜TestStatic在'exit()'而不是'dlclose()'调用?
C ++标准要求当程序以相反的构造顺序退出时,为全局对象调用析构函数。大多数实现已经通过调用C库atexit例程来处理这一点,以注册析构函数。这是有问题的,因为1999 C标准只要求实现支持32个注册的函数,虽然大多数实现支持更多。更重要的是,它并不处理大多数实现中通过在程序终止之前调用dlclose来从正在运行的程序映像中删除DSO的能力。
这个问题在GCC的后续版本中解决,包括C / C ++标准库和链接器。基本上,应该使用 __ cxa_atexit
函数而不是 atexit
(3)注册C ++析构函数。
有关 __ cxa_atexit
的完整技术详细信息,请参见 Itanium C ++ ABI规范。
从您的问题不清楚什么版本的gcc,链接器和您正在使用的标准C库。此外,您提供的代码不符合 POSIX 标准,因为没有 RTDL_NOW
或 RTDL_LOCAL
宏定义。它们是 RTLD_NOW
和 RTLD_LOCAL
(请参阅 dlopen )。
如果您的C标准库不支持 __ cxa_atexit
,您可能需要通过指定 -fno-use-cxa-atexit
gcc标志:
-fuse-cxa-atexit
为静态存储的对象注册析构函数
持续时间为_ cxa atexit
函数,而不是atexit
功能。这个选项是
完全符合标准的处理
静态析构函数所必需的,但是只有在你的C库支持
__cxa_atexit时才能使用
。
但是,这可能导致一个问题,其中析构函数以不同的顺序调用或根本不调用。因此,在破坏 __ cxa_atexit
支持或根本不支持的情况下,最佳解决方案不是在共享库中使用带有析构函数的静态对象。
plugin1.cpp:
#include <iostream>
static class TestStatic {
public:
TestStatic() {
std::cout << "TestStatic create" << std::endl;
}
~TestStatic() {
std::cout << "TestStatic destroy" << std::endl;
}
} test_static;
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;
}
build and run:
>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
why TestStatic::~TestStatic called at 'exit()' but not at 'dlclose()' ?
The C++ Standard requires that destructors be called for global objects when a program exits in the opposite order of construction. Most implementations have handled this by calling the C library atexit routine to register the destructors. This is problematic because the 1999 C Standard only requires that the implementation support 32 registered functions, although most implementations support many more. More important, it does not deal at all with the ability in most implementations to remove DSOs from a running program image by calling dlclose prior to program termination.
This problem is addressed in later versions of GCC including C/C++ standard library and linker. Basically, C++ destructors should be registered using __cxa_atexit
function instead of atexit
(3).
For the full technical details on __cxa_atexit
, see Itanium C++ ABI specification.
It is not clear from your question what version of the gcc, linker and standard C library you are using. Plus, the code you have provided does not meet POSIX standard as there are no RTDL_NOW
or RTDL_LOCAL
macros defined. They are RTLD_NOW
and RTLD_LOCAL
(see dlopen).
If your C standard library does not support __cxa_atexit
, you may need to disable it by specifying -fno-use-cxa-atexit
gcc flag:
-fuse-cxa-atexit
Register destructors for objects with static storage duration with the _cxa atexit function rather than the atexit function. This option is required for fully standards-compliant handling of static destructors, but will only work if your C library supports __cxa_atexit.
But that might result in a problem where destructors are called in different order or not called at all. So the best solution in case of broken __cxa_atexit
support or no support at all is not to use static objects with destructors in your shared libraries.
这篇关于dlclose()不会调用全局对象的析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!