[Bug c++/27319] New: C++ class object destructors are not called for static class objects in a shared library when dlclose is called.
For C++ classes of any type, the class destructor fails to be called under the following circumstances in the CYGWIN environment (not a problem in linux): 1) A static object of a class is declared within the body of a function which is part of a shared library. 2) The shared library is loaded with dlopen, the function in question is called, then the shared library is unloaded with dlclose. Sample code: // - // File mymain.C // - #include using namespace std; #include extern "C" void printit(); int main() { void * LibraryHandle = dlopen("./mylib.sl", RTLD_LAZY); if(LibraryHandle) { void (*Printit)() = ( void (*)() )dlsym(LibraryHandle, "printit"); (*Printit)(); cout<<"Close library"< extern "C" void printit(); class KStr { public: KStr(const char*); ~KStr(); char StrBuf[20]; }; KStr::KStr(const char*Str) { strcpy(StrBuf, Str); } KStr::~KStr() { cout
[Bug target/27319] C++ class object destructors are not called for static class objects in a shared library when dlclose is called.
--- Comment #2 from hbd_bugreports at earthlink dot net 2006-04-25 22:07 --- Subject: Re: C++ class object destructors are not called for static class objects in a shared library when dlclose is called. So You are telling me this is not a gcc problem but a limitation in CYGWIN. (I'm not really surprised.) ... In which case I resort to my work-around of eliminating static class objects declared in the body of functions that will be used in the shared library. Thanks for the prompt reply, Harry Dellicker pinskia at gcc dot gnu dot org wrote: >--- Comment #1 from pinskia at gcc dot gnu dot org 2006-04-25 21:31 >--- >Yes, this is a target specific issue. If the target does not support atexit, >this is what will happen. > > > > -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27319
[Bug target/27319] C++ class object destructors are not called for static class objects in a shared library when dlclose is called.
--- Comment #4 from hbd_bugreports at earthlink dot net 2006-04-25 23:46 --- Subject: Re: C++ class object destructors are not called for static class objects in a shared library when dlclose is called. pinskia at gcc dot gnu dot org wrote: >--- Comment #3 from pinskia at gcc dot gnu dot org 2006-04-25 22:14 >--- >(In reply to comment #2) > > >>Subject: Re: C++ class object destructors are not called >> for static class objects in a shared library when dlclose is called. >>So You are telling me this is not a gcc problem but a limitation in >>CYGWIN. (I'm not really surprised.) >>... In which case I resort to my work-around of eliminating static class >>objects declared in the body of functions that will be used in the >>shared library. >>Thanks for the prompt reply, >>Harry Dellicker >> >> > > >No, it is a GCC bug but it is hard to get fixed really as you need to implement >an __cxa_atexit for Windows. Now Cygwin could implement __cxa_atexit but that >does not help other windows targets. > > > > Ok, thanks for the clarification. ...But, for now, I guess I still have to resort to my workaround. :-( At least I do have a workaround. Harry -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27319