Note that there are some explanatory texts on larger screens.

plurals
  1. POc++ linux double destruction of static variable. linking symbols overlap
    primarykey
    data
    text
    <p>Environment: linux x64, compiler gcc 4.x</p> <p>Project has following structure:</p> <pre><code>static library "slib" -- inside this library, there is static object "sobj" dynamic library "dlib" -- links statically "slib" executable "exe": -- links "slib" statically -- links "dlib" dynamically </code></pre> <p>at end of the program, "sobj" is destructed twice. That behaviour is expected, BUT it is destructed twice at same memory address, i.e. same "this" in destructor - as the result there is double destruction problem. I think it is due some symbol overlapping.</p> <p>What the solution for that conflict? Maybe some linking option?</p> <hr> <p>Here is test case:</p> <hr> <p>main_exe.cpp</p> <pre><code>#include &lt;cstdlib&gt; #include "static_lib.h" #include "dynamic_lib.h" int main(int argc, char *argv[]) { stat_useStatic(); din_useStatic(); return EXIT_SUCCESS; } </code></pre> <hr> <p>static_lib.h</p> <pre><code>#ifndef STATIC_LIB_H #define STATIC_LIB_H #include &lt;cstdio&gt; void stat_useStatic(); struct CTest { CTest(): status(isAlive) { printf("CTest() this=%d\n",this); } ~CTest() { printf("~CTest() this=%d, %s\n",this,status==isAlive?"is Alive":"is Dead"); status=isDead; } void use() { printf("use\n"); } static const int isAlive=12385423; static const int isDead=6543421; int status; static CTest test; }; #endif </code></pre> <hr> <p>static_lib.cpp</p> <pre><code>#include "static_lib.h" CTest CTest::test; void stat_useStatic() { CTest::test.use(); } </code></pre> <hr> <p>dynamic_lib.h</p> <pre><code>#ifndef DYNAMIC_LIB_H #define DYNAMIC_LIB_H #include "static_lib.h" #ifdef WIN32 #define DLLExport __declspec(dllexport) #else #define DLLExport #endif DLLExport void din_useStatic(); #endif </code></pre> <hr> <p>dynamic_lib.cpp</p> <pre><code>#include "dynamic_lib.h" DLLExport void din_useStatic() { CTest::test.use(); } </code></pre> <hr> <p>CMakeLists.txt</p> <pre><code>project( StaticProblem ) cmake_minimum_required(VERSION 2.6) if(WIN32) else(WIN32) ADD_DEFINITIONS(-fPIC) endif(WIN32) ADD_LIBRARY( static_lib STATIC static_lib.cpp static_lib.h) ADD_LIBRARY( dynamic_lib SHARED dynamic_lib.cpp dynamic_lib.h) TARGET_LINK_LIBRARIES( dynamic_lib static_lib ) ADD_EXECUTABLE( main_exe main_exe.cpp ) TARGET_LINK_LIBRARIES( main_exe static_lib dynamic_lib ) </code></pre> <hr> <p>That example works OK, on windows, but on linux - there is problem. As it works ok on windows, solution should be like change some linking option or something like that, but not change project structure or not use static vars.</p> <p>Output:</p> <p>Windows</p> <pre><code>CTest() this=268472624 CTest() this=4231488 use use ~CTest() this=4231488, is Alive ~CTest() this=268472624, is Alive </code></pre> <p>Linux</p> <pre><code>CTest() this=6296204 CTest() this=6296204 use use ~CTest() this=6296204, is Alive ~CTest() this=6296204, is Dead </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload