Followers 0

# My singleton class is not so singleton

## 10 posts in this topic

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
0

##### 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.

0

##### 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.

0

##### 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
0

##### 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
0

##### 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
1

##### 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

0

##### 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.

2

##### 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

1

##### Share on other sites

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.

Sounds right. Each translation unit is compiled separately - including all code in header files copied into that TU by the preprocessor - and has code generated for it. The linker then deals with removing any duplicated definitions from all the TUs so there's only one copy of each definition in the final binaries. Link-time code generation changes up that story a little but the end result is still that by the end of the compile-time linker stage there is only a single copy of each definition (assuming you don't do something illegal to violate the ODR rule). The linker doesn't get to do its job between DLL/EXE boundaries since the code is already generated long before the DLL is loaded. Hence there's two copies of the definition between the two modules. Assuming nothing funny is going on and each TU is linked into only a single binary, moving definitions out of shared headers resolves the linking issues.
2

## Create an account

Register a new account