First of all problem context: Linux x64, gcc v4.8.5. There is an application which loads two shared libraries (let it be module1.so and module2.so), these modules has partially the same code. Now a little bit of code:
//SomeClass.h
class SomeClass
{
public:
static unsigned long& s_uObj2()
{
static unsigned long s_uObj2;
return s_uObj2;
};
void Initialize();
};
//SomeClass.cpp
void SomeClass::Initialize()
{
if (0 == s_uObj2())
{
//do init
}
s_uObj2()++; //acts as a counter
}
This code have been written long time ago and it's idea is to prevent double initialization of SomeClass in each module. The problem: this implementation somehow shares s_uObj2 value across different modules (in single application), which leads to the fact that only first module will be initialized.
How is that possible? I thought it should be isolated address space between different modules?
Please don't point me to some general case definition of "how static variables work". What I really need - is analysis of why different modules share the value of single variable in this exact case. That is because it is real project and I'm unable o refactor it all to make it work.