http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55773
Bug #: 55773 Summary: C++ class object destructors are not called which a static class object in destructor function in a shared library after dlclose is called. Classification: Unclassified Product: gcc Version: 4.1.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: liuhc...@gmail.com there is a core dump when there is a static class object in __attribute__((destructor)) function in a shared library after dlclose the shared library. ps: it is a local static class object, not a global static class object. global static class object seem not cause the problem. Sample code: // --------------------------------- // File main.c // --------------------------------- #include <stdio.h> #include <stdlib.h> #include <dlfcn.h> int main() { void * hd = dlopen("./so.so", RTLD_NOW); printf("start\n"); void (*p)() = dlsym(hd,"foo"); p(); dlclose(hd); printf("end\n"); return 0; } // --------------------------------- // File class.h // --------------------------------- #include <stdlib.h> #include <stdio.h> class Base { public: Base() { printf("constructor\n"); } ~Base() { printf("destructor\n"); } private: }; // --------------------------------- // File so.c // --------------------------------- #include "class.h" extern "C" void foo() { //static Base<int> a; } static void __attribute__((destructor)) enddll( void) { static Base a; } // ------------------------------------------ // compile line in linux // ------------------------------------------ g++ -fpic -shared so.c -o so.so gcc main.c -ldl // ------------------------------------------ // the result of running // ------------------------------------------ start constructor end セグメンテーション違反です i think the reason is that class' destructor be called after shared library be close. the class' destructor is no in memory. and a core dump happen.