Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Having problems with auto_ptr with classes

This thread is locked; no one can reply to it. rss feed Print
Having problems with auto_ptr with classes
Desmond Taylor
Member #11,943
May 2010
avatar

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.

#SelectExpand
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.
{"name":"neweo_debug_console.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/3\/9\/39b058a699b5ed97c826294f06295848.png","w":677,"h":342,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/3\/9\/39b058a699b5ed97c826294f06295848"}neweo_debug_console.png

This is with the changed lines.
{"name":"neweo_debug_console_auto_ptr_error.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/2\/a2bdcaf85cd4ebd2060f2ef374caf9e5.png","w":954,"h":589,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/2\/a2bdcaf85cd4ebd2060f2ef374caf9e5"}neweo_debug_console_auto_ptr_error.png

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?
In such case the auto_ptr shall be destroyed at the end of the scope (line 31) and thus the resource it points (the Graphics object) will be deleted.

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
avatar

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.
In the first version of your code you assign it a new object in the line 28.

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
avatar

Shouldn't you write something like:

this->gfx = std::auto_ptr<Graphics>( new Graphics );

?

[EDIT] Seems like someone was just seconds faster ...

--
"The basic of informatics is Microsoft Office." - An informatics teacher in our school
"Do you know Linux?" "Linux? Isn't that something for visually impaired people?"

Desmond Taylor
Member #11,943
May 2010
avatar

Thank you very much :) I must remember that for future :)

It now works perfectly.

Go to: