<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Having problems with auto_ptr with classes</title>
		<link>http://www.allegro.cc/forums/view/607644</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 18 Jun 2011 17:13:19 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ve just gotten into using auto_ptr so that I don&#39;t forget to free any memory and I have ran into a &quot;big&quot; problem (To me anyway).</p><p>Here is a snippet of the code involved.
</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 24</span>    <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span> <span class="s">"Engine Started... Initializing addons!\n"</span> <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 25</span>
<span class="number"> 26</span>    this-&gt;gfx <span class="k3">=</span> <span class="k1">new</span> Graphics<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 27</span>
<span class="number"> 28</span>    <span class="c">//std::auto_ptr&lt;Graphics&gt; gfx( new Graphics );</span>
<span class="number"> 29</span>
<span class="number"> 30</span>    this-&gt;isRunning <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span>
<span class="number"> 31</span><span class="k2">}</span>
<span class="number"> 32</span>
<span class="number"> 33</span>Engine::~Engine<span class="k2">(</span><span class="k2">)</span>
<span class="number"> 34</span><span class="k2">{</span>
<span class="number"> 35</span>    <span class="k1">delete</span><span class="k2">(</span> this-&gt;gfx <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 36</span>
<span class="number"> 37</span>    <a href="http://www.allegro.cc/manual/al_destroy_display"><span class="a">al_destroy_display</span></a><span class="k2">(</span> this-&gt;display <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 38</span>
<span class="number"> 39</span>    <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span> <span class="s">"Engine Stopped!\n"</span> <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 40</span><span class="k2">}</span>
</div></div><p>

If I uncomment line 28 and then comment out lines 26 &amp; 35 It causes &quot;gfx&quot; to be deleted before we even want to close the engine.</p><p>This is with using the code above.<br /><span class="remote-thumbnail"><span class="json">{"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"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/3/9/39b058a699b5ed97c826294f06295848-240.jpg" alt="neweo_debug_console.png" width="240" height="121" /></span></p><p>This is with the changed lines.<br /><span class="remote-thumbnail"><span class="json">{"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"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/a/2/a2bdcaf85cd4ebd2060f2ef374caf9e5-240.jpg" alt="neweo_debug_console_auto_ptr_error.png" width="240" height="148" /></span></p><p>Does anyone know why this happens?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Desmond Taylor)</author>
		<pubDate>Sat, 18 Jun 2011 15:19:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Isn&#39;t it the fact that after you uncomment the line 28 you actually initialize a local variable instead of a member variable?<br />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.</p><p>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).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Anton Chigurh)</author>
		<pubDate>Sat, 18 Jun 2011 16:09:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/607644/921969#target">Anton Chigurh</a> said:</div><div class="quote"><p>
If this is what is happening then making the auto_ptr a member variable should do the trick for you.
</p></div></div><p>

I really don&#39;t get what you mean there. Can you give an example?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Desmond Taylor)</author>
		<pubDate>Sat, 18 Jun 2011 16:24:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>From your code I guess that you have a variable <span class="source-code">Graphics<span class="k3">*</span> gfx<span class="k2">;</span></span> defined somewhere in your class.<br />In the first version of your code you assign it a new object in the line 28.</p><p>Then (I assume that commenting and uncommenting the mentioned lines is the only thing you do) you &quot;replace&quot; the command from the line 26 with the one from the line 28.</p><p>The problem is that the gfx declared in the line 28 is an entirely new variable, so assigning it anything won&#39;t actually affect your <span class="source-code">Graphics<span class="k3">*</span> gfx<span class="k2">;</span></span>. You instead create a new variable, local to the given function, and that local variable dies at the line 31 as it&#39;s scope ends. Note that in case of auto_ptr, &quot;dying&quot; actually means releasing the pointed resource which means your graphics object get&#39;s deleted there (line 31).</p><p>What you want to do is to change the variable declaration in the class (not visible in the example you provided) from <span class="source-code">Graphics<span class="k3">*</span></span> to `auto_ptr&lt;Graphics&gt;`. Also you must change the line 28 from an initialization to an assignment so it looks more or less like this:</p><p>`this-&gt;gfx = auto_ptr&lt;Graphics&gt;(new Graphics);`</p><p>I guess this should solve the problem of the &quot;unexplainable&quot; deallocation of the Graphics object.</p><p>Consider using a debugger and setting a breakpoint in the Graphics&#39;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 <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Anton Chigurh)</author>
		<pubDate>Sat, 18 Jun 2011 16:57:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Shouldn&#39;t you write something like:</p><div class="source-code snippet"><div class="inner"><pre>this-&gt;gfx <span class="k3">=</span> std::auto_ptr<span class="k3">&lt;</span>Graphics&gt;<span class="k2">(</span> <span class="k1">new</span> Graphics <span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

?</p><p>[EDIT] Seems like someone was just seconds faster ...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Michael Faerber)</author>
		<pubDate>Sat, 18 Jun 2011 16:58:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thank you very much <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /> I must remember that for future <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>It now works perfectly.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Desmond Taylor)</author>
		<pubDate>Sat, 18 Jun 2011 17:13:19 +0000</pubDate>
	</item>
</rss>
