Click here to Skip to main content
16,007,814 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Overlay Windows Pin
Mark Salsbery1-Dec-06 6:57
Mark Salsbery1-Dec-06 6:57 
QuestionExporting Constants in a DLL Pin
risingtechie1-Dec-06 6:01
risingtechie1-Dec-06 6:01 
AnswerRe: Exporting Constants in a DLL Pin
led mike1-Dec-06 6:19
led mike1-Dec-06 6:19 
Questionhow to input JOYSTICK to vc++ ??? Pin
denyilcode1-Dec-06 4:59
denyilcode1-Dec-06 4:59 
AnswerRe: how to input JOYSTICK to vc++ ??? Pin
aquawicket1-Dec-06 5:56
aquawicket1-Dec-06 5:56 
GeneralRe: how to input JOYSTICK to vc++ ??? Pin
Mark Salsbery1-Dec-06 7:02
Mark Salsbery1-Dec-06 7:02 
AnswerRe: how to input JOYSTICK to vc++ ??? Pin
Mark Salsbery1-Dec-06 7:11
Mark Salsbery1-Dec-06 7:11 
QuestionSingleton template and dll Pin
Cedric Moonen1-Dec-06 4:34
Cedric Moonen1-Dec-06 4:34 
Hello,

I came across a strange problem. I have a dll which creates an instance of a class. This class inherits from a template singleton.

The singleton template:

template <class T>
class CSingleton
{
public:
	static T* GetInstance()
	{
		if (!m_pInstance)
			m_pInstance = new T;
		return m_pInstance;
	}

	void Destroy()
	{
		if (m_pInstance)
			delete m_pInstance;
		m_pInstance = NULL;
	}

protected:
	CSingleton()  { }
	virtual ~CSingleton() { }

private:
	static T* m_pInstance;
};

template <class T> T* CSingleton<T>::m_pInstance = NULL;


The class is a simple empty class:
class CMyClass : public CSingleton<CMyClass>
{
	friend CSingleton<CMyClass>;

protected:
	CMyClass();
	~CMyClass();
};


And then I have an exported function that creates and return the instance of this class:

#ifdef DLL_EXPORTS
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif // DLL_EXPORT

DLL_API CMyClass* CreateClass();

(the function simply call GetInstance of CMyClass and returns the pointer)

When I call this function from the code of my executable, it returns well a pointer to the instance but the strange thing is that the static m_pInstance member of CSingleton is NULL.
I used the debugger and this member is correct inside the CreateClass function but as soon as I come back in the code of the executable, then this member is NULL.

It means that if I try to call Destroy, the memory won't be released. Worse, if I call GetInstance, I will certainly have an unexpected behavior (the instance will be created a second time)

It has probably something to do with the fact that this member is static. But I don't understand clearly what's happening there. Is there a workaround ?

Thank you.


Cédric Moonen
Software developer

Charting control [v1.1]

AnswerRe: Singleton template and dll Pin
Rick York1-Dec-06 8:04
mveRick York1-Dec-06 8:04 
GeneralRe: Singleton template and dll [modified] Pin
Cedric Moonen1-Dec-06 8:12
Cedric Moonen1-Dec-06 8:12 
GeneralRe: Singleton template and dll Pin
CPallini1-Dec-06 9:56
mveCPallini1-Dec-06 9:56 
GeneralRe: Singleton template and dll Pin
Cedric Moonen1-Dec-06 10:19
Cedric Moonen1-Dec-06 10:19 
GeneralRe: Singleton template and dll Pin
led mike1-Dec-06 10:22
led mike1-Dec-06 10:22 
GeneralRe: Singleton template and dll Pin
Cedric Moonen3-Dec-06 22:18
Cedric Moonen3-Dec-06 22:18 
GeneralRe: Singleton template and dll Pin
led mike4-Dec-06 5:48
led mike4-Dec-06 5:48 
GeneralRe: Singleton template and dll Pin
Cedric Moonen4-Dec-06 8:03
Cedric Moonen4-Dec-06 8:03 
GeneralRe: Singleton template and dll Pin
led mike4-Dec-06 9:06
led mike4-Dec-06 9:06 
GeneralRe: Singleton template and dll Pin
Cedric Moonen4-Dec-06 9:25
Cedric Moonen4-Dec-06 9:25 
GeneralRe: Singleton template and dll Pin
led mike4-Dec-06 11:00
led mike4-Dec-06 11:00 
GeneralRe: Singleton template and dll Pin
Cedric Moonen4-Dec-06 20:15
Cedric Moonen4-Dec-06 20:15 
GeneralRe: Singleton template and dll Pin
led mike5-Dec-06 6:03
led mike5-Dec-06 6:03 
GeneralRe: Singleton template and dll Pin
Cedric Moonen5-Dec-06 20:31
Cedric Moonen5-Dec-06 20:31 
GeneralRe: Singleton template and dll Pin
led mike6-Dec-06 6:10
led mike6-Dec-06 6:10 
GeneralRe: Singleton template and dll Pin
Cedric Moonen6-Dec-06 8:11
Cedric Moonen6-Dec-06 8:11 
GeneralRe: Singleton template and dll Pin
Rick York1-Dec-06 10:25
mveRick York1-Dec-06 10:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.