Skip to main content
GameDev.net gamedev.net
🔒 Locked

Singleton

Started by Hippokrates Jul 19, 2003 at 9:25 AM 4 replies 1.1k views
Original Post
Hippokrates
Hippokrates
I have tried to implement the singleton design pattern. I chose the usual(?) template version which can be inherited. But I had to find out that it does not work with DLLs. If I do my singleton like this:
template 
class CSingleton
{
    T * Get();
...
};

class CConcreteSingleton : public CSingleton
{
...
};
 
The Get() Method will return different pointers in the application and in the DLL. But if I wrap my Direct3D Device or whatever within a singleton it should be the same in the DLL as in the application. Why does it not work?
Im Anfang war die Tat...Faust
Lorenzo
Lorenzo
quote:
Original post by Hippokrates
I have tried to implement the singleton design pattern. I chose the usual(?) template version which can be inherited. But I had to find out that it does not work with DLLs.

If I do my singleton like this:

template <class T>
class CSingleton
{
T * Get();
...
};

class CConcreteSingleton : public CSingleton<ConcreteType>
{
...
};

The Get() Method will return different pointers in the application and in the DLL.
But if I wrap my Direct3D Device or whatever within a singleton it should be the same in the DLL as in the application.

Why does it not work?


cause statics and globals are not initialized in a DLL. They are inizialized in mainCRTStarup (which eventually calls your real main after doing such things) in an EXE, I don''t know any similar for dll''s

evolutional
evolutional
Can you not pass an address pointer to your singleton and use it like that?
Hippokrates
Hippokrates
You mean like:

CSingleton::SetPtr(new int);

?

Im Anfang war die Tat...Faust
evolutional
evolutional
In your DLL, have some code that initialises it.


Singleton *gSingle

BOOL
Init(Singleton *single) { gSingle = single; }


Use this to store the singleton pointer somewhere within the DLL for future use.

Singleton Singleton::m_single = 0;

Then in your app... dllhandle->Init(&Sigleton::m_single)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.