Original Post
In order to get to the problem I will have to "set the scene" a little. Please bear with me... I have the following Singleton template class which resides in a static library (.lib). In this static library I also have a CLogger class which inherits from this Singleton class. In my application (.exe) I link to this .lib and instantiate a CLogger class. The .exe also links to a .dll at runtime and this dll contains the following code which is causing me the problem... The problem is that the previous bit of code causes an assertion because the m_Singleton member is null. However, in the .exe, I have the following bit of code which does work as the log is created and written to... Can anyone explain to me why this is happening and how I might be able to solve this problem?
template <typename T>
class CSingleton
{
protected:
static T* m_Singleton;
public:
// Constructor
CSingleton()
{
assert(!m_Singleton);
#if defined( _MSC_VER ) && _MSC_VER < 1200
int Offset = (int)(T*)1 - (int)(CSingleton<T>*)(T*)1;
m_Singleton = (T*)((int)this + Offset);
#else
m_Singleton = static_cast<T*>(this);
#endif
}
// Destructor
~CSingleton()
{
assert(m_Singleton);
m_Singleton = NULL;
}
static T& GetSingleton()
{
assert(m_Singleton);
return *m_Singleton;
}
static T* GetSingletonPtr()
{
assert(m_Singleton);
return m_Singleton;
}
};
template <typename T> T* CSingleton <T>::m_Singleton = NULL;
CLogger::GetSingleton()->Log("D3D9Graphics.dll", "CD3D9Graphics",
"Init", "Failed to create Direct3DDevice9");
m_Logger.GetSingleton().CreateLog("log.txt");