![]() |
|
Having problems with auto_ptr with classes |
Desmond Taylor
Member #11,943
May 2010
![]() |
I've just gotten into using auto_ptr so that I don't forget to free any memory and I have ran into a "big" problem (To me anyway). Here is a snippet of the code involved. 24 printf( "Engine Started... Initializing addons!\n" );
25
26 this->gfx = new Graphics();
27
28 //std::auto_ptr<Graphics> gfx( new Graphics );
29
30 this->isRunning = true;
31}
32
33Engine::~Engine()
34{
35 delete( this->gfx );
36
37 al_destroy_display( this->display );
38
39 printf( "Engine Stopped!\n" );
40}
If I uncomment line 28 and then comment out lines 26 & 35 It causes "gfx" to be deleted before we even want to close the engine. This is with using the code above. This is with the changed lines. Does anyone know why this happens? |
Anton Chigurh
Member #12,964
June 2011
|
Isn't it the fact that after you uncomment the line 28 you actually initialize a local variable instead of a member variable? If this is what is happening then making the auto_ptr a member variable should do the trick for you. You may also want to read about other kinds of smart pointers provided by the boost library (they may however turn out an overkill for your needs). |
Desmond Taylor
Member #11,943
May 2010
![]() |
Anton Chigurh said: If this is what is happening then making the auto_ptr a member variable should do the trick for you. I really don't get what you mean there. Can you give an example? |
Anton Chigurh
Member #12,964
June 2011
|
From your code I guess that you have a variable Graphics* gfx; defined somewhere in your class. Then (I assume that commenting and uncommenting the mentioned lines is the only thing you do) you "replace" the command from the line 26 with the one from the line 28. The problem is that the gfx declared in the line 28 is an entirely new variable, so assigning it anything won't actually affect your Graphics* gfx;. You instead create a new variable, local to the given function, and that local variable dies at the line 31 as it's scope ends. Note that in case of auto_ptr, "dying" actually means releasing the pointed resource which means your graphics object get's deleted there (line 31). What you want to do is to change the variable declaration in the class (not visible in the example you provided) from Graphics* to `auto_ptr<Graphics>`. Also you must change the line 28 from an initialization to an assignment so it looks more or less like this: `this->gfx = auto_ptr<Graphics>(new Graphics);` I guess this should solve the problem of the "unexplainable" deallocation of the Graphics object. Consider using a debugger and setting a breakpoint in the Graphics's destructor, and then debug step by step the function part of which we can see in the lines 24 through 31. Try establishing, when the destructor gets called, so that you can confirm if what I say is actually true |
Michael Faerber
Member #4,800
July 2004
![]() |
Shouldn't you write something like: this->gfx = std::auto_ptr<Graphics>( new Graphics ); ? [EDIT] Seems like someone was just seconds faster ... -- |
Desmond Taylor
Member #11,943
May 2010
![]() |
Thank you very much It now works perfectly. |
|