G
Guest
Hi All,
I'm having some trouble with a .lib file in a project I'm working on.
The file contains a class which implements the singleton pattern.
To implement it I've declared a static pointer to the class called instance
outside of the class. The code looks something like this(the project is very
large and I haven't been able to build a cut-down version of the problem
because I'm not familiar with all the settings):
static Singleton* instance = 0;
class Singleton
{
public:
static Singleton* getInstance()
{
if(instance == 0)
{
instance = new Singleton();
}
return instance;
}
.....
}
So other classes that wish to use the class call getInstance() which
instantiates a new instance or returns the currently existing one.
Now the problem seems to arise because two DLL's link to the library
containing the class. So when one dll calls the getInstance method he get's
one instance (always the same for inside that dll) but when the second one
calls it he gets a new instance at an entirely new address....
I've tried changing the way my singleton is implemented but have not been
able to implement it using any other method so far. I don't even really think
that that is the problem. I'm starting to think that no matter how I
implement the class it's going to be returning two different instances of the
class.
Has anyone encountered anything like this before? I have a feeling that it
is some sort of linker or VC++ specific problem that I'm having.
Cheers
Johnny
I'm having some trouble with a .lib file in a project I'm working on.
The file contains a class which implements the singleton pattern.
To implement it I've declared a static pointer to the class called instance
outside of the class. The code looks something like this(the project is very
large and I haven't been able to build a cut-down version of the problem
because I'm not familiar with all the settings):
static Singleton* instance = 0;
class Singleton
{
public:
static Singleton* getInstance()
{
if(instance == 0)
{
instance = new Singleton();
}
return instance;
}
.....
}
So other classes that wish to use the class call getInstance() which
instantiates a new instance or returns the currently existing one.
Now the problem seems to arise because two DLL's link to the library
containing the class. So when one dll calls the getInstance method he get's
one instance (always the same for inside that dll) but when the second one
calls it he gets a new instance at an entirely new address....
I've tried changing the way my singleton is implemented but have not been
able to implement it using any other method so far. I don't even really think
that that is the problem. I'm starting to think that no matter how I
implement the class it's going to be returning two different instances of the
class.
Has anyone encountered anything like this before? I have a feeling that it
is some sort of linker or VC++ specific problem that I'm having.
Cheers
Johnny