In my GUI application (MFC) I am using a dll to display something in the screen. I have a static library which having a singleton class for this.
eg: sing.lib. I am including sing.lib in application (exe) project and in dll project(coz both uses this singleton class)
Issue is the instance getting in exe and in dll is different. Both calls the constructor!!
see the singleton class code snippet.
class A
{
private:
A();
virtual ~A();
static A* m_pInstance;
public:
static A* GetInstance()
{
if (NULL == m_pInstance)
{
m_pInstance = new A();
}
return m_pInstance;
}
}
If you want the singleton instance to be shared between dll and exe, place its definition in a dynamic link library instead of static library.
In general if you want some data to be global and unique you should not put it in static library.
Consider
such a code in static library. In your case if both the exe and dll link against this library each will get its own CurrentCounter. So exe and dll can have different values of CurrentCounter at the same time.