I'm currently porting my project from Windows to Linux.
The project consists of a 'main' shared library, several plugins (also shared libraries) and a launcher application.
Within the 'main' shared library there's a template singleton class another class can inherit from to use the singleton pattern.
The template singleton class is implemented as follows:
template<class T>
class Singleton
{
public:
static T* getInstance()
{
if(!m_Instance)
{
m_Instance = new T();
}
return m_Instance;
}
private:
static T* m_Instance;
protected:
Singleton()
{
assert(!m_Instance);
m_Instance = (T*)this;
}
~Singleton()
{
m_Instance = 0;
}
};
template<class T> T* Singleton<T>::m_Instance = 0;
A class that inherits from the Singleton class is - for example - the Logger class.
So whenever I call
Logger::getInstance()
I get a valid instance of the logger class.
For windows this works across multiple DLLs.
If I instantiate the logger in the 'main' dll and try to get the instance in plugin A and B, it'll always return the same instance.
On Linux however I can not reproduce this behavior. For both plugin A and B the assert
assert(!m_Instance);
triggers and the program stops.
What do I have to do to get the same behavior as I have in Windows with dlls?
I tried linking with -rdynamic but unfortunately this didn't solve the problem.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…