Static variable corruption on shared library loading
-
Could anyone explain why loading shared library corrupts static variable?
-
Output on Linux (AltLinux, openSUSE): 1 true 0
-
Output on Windows (using MinGW): 1 true 1
Qt 5.3.1: https://github.com/ipostanogov/variables-destroyer
// Not satisfied with answers @ http://stackoverflow.com/q/26317163
-
-
I've tested your example in Arch Linux (gcc 4.9.1 and clang 3.5.0, Qt 4.8.6 and 5.3.2) and it give me 1 true 1 that seems to be the right solution. Its maybe a bug of your compiler, it could be also that when you load the plugin you are redefining the Core::list variable in "this line":https://github.com/ipostanogov/variables-destroyer/blob/master/core/core.cpp#L3.
-
global variables (which you use there) are accessed by names and all names are exported on Linux. This implies that each global name MUST exist only once. E.g. havin a shared library with a global variable
@
int myVal;
@and another library with
@
QString myVal;
@it is undefined which one exists in the process memory. It can be any of these but only one. On Windows, the behavior is different, as only exported names are globally.