views:

236

answers:

2

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?

A: 

I believe that STL classes are always dynamically created so you can't actually call them static. They exist on the heap. If the member is passed to a function then a copy is put into static memory. You have to make your own destructor that deletes the stl explicitly once.

datdo
By static, I mean statically linked (e.g. that my main program contains a copy of the code for class 'A' and my .so contains a copy of the code for class 'A') with the static prefix.For Example: Class A { public: static list<string> slist; }
Paul
A: 

This question has been resolved in another question I posted. Basically there were indeed two copies of the static variable -- one in the main program and one in the shared library, but the runtime linker was resolving both copies to the main programs copy. See this question for more information:

http://stackoverflow.com/questions/2631918/main-program-and-shared-library-initializes-same-static-variable-in-static-init

Paul