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.
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"}
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"}
Does anyone know why this happens?
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).
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?
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 .
Shouldn't you write something like:
this->gfx = std::auto_ptr<Graphics>( new Graphics );
?
[EDIT] Seems like someone was just seconds faster ...
Thank you very much I must remember that for future
It now works perfectly.