Static class variables in a dynamic library and main program

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

I have 2 questions, namely:
1) In this particular case, why aren't there two copies of the static variable (yes, I know, that sounds a bit ridiculous, but since both the main program and the .so file have separately compiled β€œA”, shouldn't they be each one?)
2) Is there a way to solve this problem without re-writing class "A" so as not to contain static member variables?

+4
source share
2 answers

This issue was resolved in another issue that I posted. There were basically two copies of the static variable β€” one in the main program and one in the shared library, but the run-time linker allowed both copies of the copy of the main program. See this question for more information:

The main program and the shared library initialize the same static variable in __static_initialization_and_destruction_0

+2
source

I believe that STL classes are always dynamically created, so you cannot call them static. They exist on the heap. If the element is passed to the function, then the copy is placed in static memory. You must create your own destructor that removes stl explicitly once.

0
source

Source: https://habr.com/ru/post/1306733/


All Articles