Jump to content

  • Log In with Google      Sign In   
  • Create Account

My singleton class is not so singleton


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 noatom   Members   -  Reputation: 785

Like
0Likes
Like

Posted 01 February 2014 - 01:05 PM

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?

 

f57.jpg


Edited by noatom, 01 February 2014 - 02:26 PM.


Sponsor:

#2 richardurich   Members   -  Reputation: 1187

Like
0Likes
Like

Posted 01 February 2014 - 01:22 PM

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.



#3 iMalc   Crossbones+   -  Reputation: 2314

Like
0Likes
Like

Posted 01 February 2014 - 01:36 PM

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.


"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

#4 rip-off   Moderators   -  Reputation: 8763

Like
3Likes
Like

Posted 01 February 2014 - 01:47 PM

Post a complete program demonstrating the behaviour.

 

Mandatory comment about singletons being overrated and overused...



#5 GuardianX   Crossbones+   -  Reputation: 1527

Like
0Likes
Like

Posted 01 February 2014 - 02:10 PM

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


Edited by GuardianX, 01 February 2014 - 02:11 PM.


#6 noatom   Members   -  Reputation: 785

Like
0Likes
Like

Posted 01 February 2014 - 02:27 PM

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, 01 February 2014 - 02:28 PM.


#7 Aardvajk   Crossbones+   -  Reputation: 6256

Like
1Likes
Like

Posted 01 February 2014 - 02:53 PM

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, 01 February 2014 - 02:55 PM.


#8 noatom   Members   -  Reputation: 785

Like
0Likes
Like

Posted 01 February 2014 - 03:21 PM

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



#9 Aardvajk   Crossbones+   -  Reputation: 6256

Like
2Likes
Like

Posted 01 February 2014 - 03:31 PM

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.



#10 noatom   Members   -  Reputation: 785

Like
1Likes
Like

Posted 01 February 2014 - 03:36 PM

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



#11 SeanMiddleditch   Members   -  Reputation: 7261

Like
2Likes
Like

Posted 02 February 2014 - 02:01 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS