Static Class Variables in Dynamic Library and Main Program

Posted by Paul on Stack Overflow See other posts from Stack Overflow or by Paul
Published on 2010-04-12T19:43:39Z Indexed on 2010/04/13 3:42 UTC
Read the original article Hit count: 342

I am working on a project that has a class 'A' that contains a static stl container class. This class is included in both my main program and a .so file. The class uses the default(implicit, not declared) constructor/destructor. The main program loads the .so file using dlopen() and in its destructor, calls dlclose(). The program crashes after main exits when glibc calls the destructor for the static class member variable. The problem appears to be that when dlclose() is called, the destructor for the static variable is called, then when main exits() glibc also calls the destructor, resulting in a double free.

I have 2 questions, namely:
1) In this particular case, why are there not two copies of the static variable(yes i know that sounds somewhat ridiculous, but since both the main program and .so file have a separately compiled 'A', shouldn't they each have one?)
2) Is there any way to resolve this issue without re-writing class 'A' to not contain static member variables?

© Stack Overflow or respective owner

Related posts about c++

Related posts about shared-libraries