# My singleton class is not so singleton

This topic is 2083 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Here it is:

	class PhysicsModule{

int AAA;

private:
PhysicsModule() {};
PhysicsModule(PhysicsModule const&) {};
void operator=(PhysicsModule const&) {};
public:

static PhysicsModule* getInstance(){
static PhysicsModule instance;
return &instance;
}

bool Initialize() {AAA = 12;return true;}

};


So,the problem is,if I call PhysicsModule::getInstance()->Initialize(); it should initialize AAA  to 12 for the singleton.

But if after that I do:

PhysicsModule* test = PhysicsModule::getInstance();

The AAA pointed to by test won't be 12,it will be 0!

Some help?

Edited by noatom

##### Share on other sites

Are you sure you entered the right code? I copy/pasted that, added public in front of AAA, added return true to Init(), and put the two lines of code in main() and printed out the value of AAA in the second access, and it's 12.

##### Share on other sites

How are you checking the value of AAA afterwards? I note that it's private and not exposed through any methods, so presumably you are using the debugger to view the value?

Is it possible then that your debugger is showing you the value of a similarly named local variable instead?

Even if not, debuggers are known to get it wrong sometimes.

I recommend fixing the mistake richardurich noted, and then posting a full example that can be compiled. Of course if you solve the problem in the process then that's all good too.

##### Share on other sites

Post a complete program demonstrating the behaviour.

Mandatory comment about singletons being overrated and overused...

##### Share on other sites

The sequence of actions you describe actually leads to correct results (AAA is 12). Check it out: http://ideone.com/M87EBl

Edited by GuardianX

##### Share on other sites

Em...I tested it with a: if aaa == 12,print something,and it turns out it's not equal to 12

I use __declspec(dllexport) since I have my class in a dll,so I can use the functions outside the dll,could that cause any problem?

And the code is pretty much what I showed in the first post,nothing else.

Edited by noatom

##### Share on other sites

Em...I tested it with a: if aaa == 12,print something,and it turns out it's not equal to 12

I use __declspec(dllexport) since I have my class in a dll,so I can use the functions outside the dll,could that cause any problem?

And the code is pretty much what I showed in the first post,nothing else.

Well the code you posted works, and there must be more as you are calling getInstance() and testing the value and so on. Your posted code is fine, we need more information.

This is a bad idea in any case though, making a phyiscs subsystem a singleton. Yes, you only need one but what you really don't want is uncontrolled access to the physics subsystem across your entire codebase. Just make it an instance and pass it around.

Edited by Aardvajk

##### Share on other sites

It must have something to do with __declspec(dllexport)

I normally call Initialize() from an app that uses the dll,and later try to access the AAA IN THE dll,that's when I get AAA=0;

BUT,if I try to access AAA in the app,I get AAA = 12

##### Share on other sites

It must have something to do with __declspec(dllexport)

I normally call Initialize() from an app that uses the dll,and later try to access the AAA IN THE dll,that's when I get AAA=0;

BUT,if I try to access AAA in the app,I get AAA = 12

Makes more sense. Someone with more expertise about all this can confirm, but I assume that because the method is inline (i.e. defined in the class header) the static member is created in both the app and the DLL. You need to be careful using static in inline methods across a DLL boundary. Move the definition of the method into the .cpp and it should be fine.

##### Share on other sites

Yes,that was the problem. I don't usually define functions in headers...but this time I did,and look what happened!

Thanks a lot

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 11
• 23
• 42
• 75