<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>An odd crash ...</title>
		<link>http://www.allegro.cc/forums/view/588018</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 15 Oct 2006 22:26:15 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I whipped up a quick little program to test some ideas I have been thinking of, and was rather dismayed when it segfaults when closing.</p><p>The source:
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>&#160;</td></tr><tr><td class="number">2</td><td><span class="k1">int</span> CFrame::LoadFrame<span class="k2">(</span><span class="k1">char</span> <span class="k3">*</span>path, <span class="k1">int</span> ID<span class="k2">)</span></td></tr><tr><td class="number">3</td><td><span class="k2">{</span></td></tr><tr><td class="number">4</td><td>    bmpFrame <span class="k3">=</span> <a href="http://www.allegro.cc/manual/load_bmp" target="_blank"><span class="a">load_bmp</span></a><span class="k2">(</span>path,NULL<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">5</td><td>    intFrameID <span class="k3">=</span> id</td></tr><tr><td class="number">6</td><td>&#160;</td></tr><tr><td class="number">7</td><td>    <span class="k1">if</span><span class="k2">(</span><span class="k3">!</span>bmpFrame<span class="k2">)</span> <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">8</td><td>    </td></tr><tr><td class="number">9</td><td>    <span class="k1">return</span> <span class="n">1</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td><span class="k2">}</span></td></tr><tr><td class="number">11</td><td>&#160;</td></tr><tr><td class="number">12</td><td><span class="c">//not anywhere near completed yet</span></td></tr><tr><td class="number">13</td><td><span class="k1">void</span> CAnimation::LoadFrames<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">14</td><td><span class="k2">{</span></td></tr><tr><td class="number">15</td><td>    <span class="k1">if</span><span class="k2">(</span><span class="k3">!</span>frameArray<span class="k2">[</span><span class="n">0</span><span class="k2">]</span>.LoadFrame<span class="k2">(</span><span class="s">"media/Walk1.bmp"</span>, <span class="n">1</span><span class="k2">)</span><span class="k2">)</span><span class="k2">{</span><a href="http://www.allegro.cc/manual/allegro_message" target="_blank"><span class="a">allegro_message</span></a><span class="k2">(</span><span class="s">"ERROR    </span></td></tr><tr><td class="number">16</td><td><span class="s">    Loading Image"</span><span class="k2">)</span><span class="k2">;</span> <span class="k2">}</span></td></tr><tr><td class="number">17</td><td>    <span class="k1">if</span><span class="k2">(</span><span class="k3">!</span>frameArray<span class="k2">[</span><span class="n">1</span><span class="k2">]</span>.LoadFrame<span class="k2">(</span><span class="s">"media/Walk2.bmp"</span>, <span class="n">2</span><span class="k2">)</span><span class="k2">)</span><span class="k2">{</span><a href="http://www.allegro.cc/manual/allegro_message" target="_blank"><span class="a">allegro_message</span></a><span class="k2">(</span><span class="s">"ERROR </span></td></tr><tr><td class="number">18</td><td><span class="s">    Loading Image"</span><span class="k2">)</span><span class="k2">;</span> <span class="k2">}</span></td></tr><tr><td class="number">19</td><td><span class="k2">}</span></td></tr><tr><td class="number">20</td><td>&#160;</td></tr><tr><td class="number">21</td><td><span class="c">// --snip--</span></td></tr></tbody></table></div></div><p>
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">2</td><td><span class="k2">{</span></td></tr><tr><td class="number">3</td><td>  <a href="http://www.allegro.cc/manual/allegro_init" target="_blank"><span class="a">allegro_init</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">4</td><td>  <a href="http://www.allegro.cc/manual/install_keyboard" target="_blank"><span class="a">install_keyboard</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">5</td><td>  </td></tr><tr><td class="number">6</td><td>  <a href="http://www.allegro.cc/manual/set_color_depth" target="_blank"><span class="a">set_color_depth</span></a><span class="k2">(</span><span class="n">24</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">7</td><td>  <a href="http://www.allegro.cc/manual/set_gfx_mode" target="_blank"><span class="a">set_gfx_mode</span></a><span class="k2">(</span>GFX_AUTODETECT_WINDOWED, <span class="n">640</span>,<span class="n">480</span>,<span class="n">0</span>,<span class="n">0</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">8</td><td>  </td></tr><tr><td class="number">9</td><td>  </td></tr><tr><td class="number">10</td><td>  </td></tr><tr><td class="number">11</td><td>  CAnimation Animation1<span class="k2">;</span></td></tr><tr><td class="number">12</td><td>  </td></tr><tr><td class="number">13</td><td>  Animation1.LoadFrames<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">14</td><td>  Animation1.clean<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">15</td><td>  </td></tr><tr><td class="number">16</td><td>  <a href="http://www.allegro.cc/manual/readkey" target="_blank"><span class="a">readkey</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">17</td><td>  </td></tr><tr><td class="number">18</td><td>  </td></tr><tr><td class="number">19</td><td>  <span class="k1">return</span> <span class="n">1</span><span class="k2">;</span></td></tr><tr><td class="number">20</td><td><span class="k2">}</span></td></tr><tr><td class="number">21</td><td><a href="http://www.allegro.cc/manual/END_OF_MAIN" target="_blank"><span class="a">END_OF_MAIN</span></a><span class="k2">(</span><span class="k2">)</span></td></tr></tbody></table></div></div><p>

So all I essentially do is load two tiny sprites into an array (which works apparently).  I do not draw them (yet), I just have them there.  As soon as I press a key to leave, it segfaults.</p><p>I notice that if I change &#39;readkey()&#39; to <span class="source-code"> <span class="k1">while</span><span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_ESC<span class="k2">]</span><span class="k2">)</span><span class="k2">{</span><span class="k2">}</span></span> the segfault seems to miraculously disappear.</p><p>Any reasons why?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Samuel Henderson)</author>
		<pubDate>Fri, 13 Oct 2006 09:39:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>bmpFrame is a BITMAP*?<br />And sorry, but I don&#39;t understand your CAnimation::LoadFrames() method:-/
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paul whoknows)</author>
		<pubDate>Fri, 13 Oct 2006 09:58:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeah, basically framesArray[] is an array of &#39;Frame&#39;, which has a BITMAP *bmpFrame; inside it.</p><p>edit:<br />So what the CAnimation::LoadFrames() does is load the frames with the required bitmaps.  It&#39;s hardcoded and whatnot, but I was just testing out some ideas.</p><p>edit2:</p><p>I am using Windows XP Pro SP2 btw.  Not sure if the OS would make a difference here...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Samuel Henderson)</author>
		<pubDate>Fri, 13 Oct 2006 10:01:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Lets see CAnimation::clean().</p><p>Also, why return 1?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Fri, 13 Oct 2006 18:26:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Lets see CAnimation::clean().</p><p>Also, why return 1?
</p></div></div><p>

Well, return 1; was changed from return 0;   I was kind of flabergasted for a bit and started changing things to try and nail down the problem.  That was actually why the whole &#39;clean()&#39; methods were developed.  CFrame has a clean method which basically checks to see if the bmpFrame is empty or not.  If it is not empty then the bitmap is destroyed. CAnimation has a method of the same name which goes through each frame in it&#39;s frameArray calling the clean() method of each frame. The segfault was appearing before I added this, so I don&#39;t think it was relevant.</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> CFrame::clean<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
    <span class="k1">if</span><span class="k2">(</span>bmpFrame<span class="k2">)</span> <a href="http://www.allegro.cc/manual/destroy_bitmap" target="_blank"><span class="a">destroy_bitmap</span></a><span class="k2">(</span>bmpFrame<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>

<span class="k1">void</span> CAnimation::clean<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
    <span class="k1">int</span> i<span class="k3">=</span><span class="n">0</span><span class="k2">;</span>
    <span class="k1">for</span><span class="k2">(</span>i<span class="k2">;</span>i<span class="k3">&lt;</span>intNumFrames<span class="k2">;</span>i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span>
    <span class="k2">{</span>
        frameArray<span class="k3">&lt;</span>i&gt;.clean<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
    <span class="k2">}</span>
<span class="k2">}</span>
</pre></div></div><p>

What I am doing is making an OOP animation system.  There was a thread <a href="http://www.allegro.cc/forums/thread/586262"> here </a> about the whole thing.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Samuel Henderson)</author>
		<pubDate>Fri, 13 Oct 2006 19:39:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Got destructor?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Wetimer)</author>
		<pubDate>Sat, 14 Oct 2006 01:02:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Got destructor?
</p></div></div><p>

Well, no.  The clean() methods are supposed to mimic one. I remember using destroy_bitmap(blah) in destructors before and it caused segfaults when exiting as well.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Samuel Henderson)</author>
		<pubDate>Sat, 14 Oct 2006 01:34:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>When I destroy_bitmap(bVariable), I also set bVariable back to NULL, that way, if for some reason clean() is called after a previous clean(), it won&#39;t try to destroy bad data.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Onewing)</author>
		<pubDate>Sat, 14 Oct 2006 02:07:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
When I destroy_bitmap(bVariable), I also set bVariable back to NULL, that way, if for some reason clean() is called after a previous clean(), it won&#39;t try to destroy bad data.
</p></div></div><p>

I&#39;ll make a note to do that.  I still would like to know what caused that segfault...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Samuel Henderson)</author>
		<pubDate>Sat, 14 Oct 2006 02:29:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Is it still just caused by the readkey() function?  Try commenting out other lines with the readkey() still there to see if it keeps segfaulting.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Onewing)</author>
		<pubDate>Sat, 14 Oct 2006 02:33:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well,  when I commented out the loading of the images it did not crash.  This confuses me because the images loaded fine as far as I can tell.  I didn&#39;t actually draw them or even use them in any way.</p><p>I find it weird that when I uncomment the image loading and comment out the readkey() and replace with the conditional loop the problem disappears. <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" /></p><p>edit:<br />Perhaps these are the code demons my teacher was talking/joking about <img src="http://www.allegro.cc/forums/smileys/shocked.gif" alt=":o" /><img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Samuel Henderson)</author>
		<pubDate>Sat, 14 Oct 2006 02:37:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Try not putting the class on the stack. Either make it global, or do:
</p><div class="source-code snippet"><div class="inner"><pre>CAnimation <span class="k3">*</span>Animation1 <span class="k3">=</span> <span class="k1">new</span> CAnimation<span class="k2">;</span>
...
<span class="k1">delete</span> Animation1<span class="k2">;</span>
<span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Sat, 14 Oct 2006 02:52:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>hmm after doing what you said to do Kitty Cat an &#39;Application Error&#39; occurs.</p><p>I&#39;ll upload a picture of my error.  It is different then before, because before I would get the whole send/don&#39;t send error report.  I would also have to manually kill the process from task manager before I could recompile before.  Now the process seems to be taken out automatically.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Samuel Henderson)</author>
		<pubDate>Sat, 14 Oct 2006 03:20:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>What is frameArray?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Sat, 14 Oct 2006 03:29:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>frameArray is an array of Frames <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><div class="source-code snippet"><div class="inner"><pre><span class="k1">class</span> CAnimation
<span class="k2">{</span>
    private:
       CFrame frameArray<span class="k2">[</span><span class="n">2</span><span class="k2">]</span><span class="k2">;</span>
    <span class="c">//...</span>

<span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Samuel Henderson)</author>
		<pubDate>Sat, 14 Oct 2006 03:32:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>What&#39;s intNumFramesset to? If it&#39;s uninitialized garbage, it&#39;s probably trying to clean more frames than are really there.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Sat, 14 Oct 2006 03:57:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It&#39;s actually been initialized with a value of two... I do so in the LoadFrames() method.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Samuel Henderson)</author>
		<pubDate>Sat, 14 Oct 2006 04:06:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Try leaving out the calls to shutdown allegro.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Wetimer)</author>
		<pubDate>Sat, 14 Oct 2006 12:48:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
if(bmpFrame) destroy_bitmap(bmpFrame);
</p></div></div><p>
When will people realise that destroy_bitmap() deals fine with NULL?<br />And do set pointers to NULL after freeing them. It will prevent crashes from double frees or invalid pointer dereferences.</p><p>About your problem: make sure you don&#39;t call Allegro functions from global destructors (I didn&#39;t read the whole thread, so maybe this was proposed before).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Sat, 14 Oct 2006 14:06:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Add printf&#39;s in your cleaning/destructing code to see how far it goes before segfaulting, that&#39;s the easiest way I know to fix those bugs.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
When will people realise that destroy_bitmap() deals fine with NULL?
</p></div></div><p>
It does? In what way? if(!bmp) return; ?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ron Ofir)</author>
		<pubDate>Sun, 15 Oct 2006 22:11:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">man destroy_bitmap said:</div><div class="quote"><p>

destroy_bitmap(3)               Allegro manual               destroy_bitmap(3)</p><p>NAME<br />       destroy_bitmap - Destroys any type of created bitmap. Allegro game pro-<br />       gramming library.</p><p>SYNOPSIS<br />       #include &lt;allegro.h&gt;</p><p>       void destroy_bitmap(BITMAP *bitmap);</p><p>DESCRIPTION<br />       Destroys a memory bitmap, sub-bitmap, video memory  bitmap,  or  system<br />       bitmap  when  you are finished with it. <b>If you pass a NULL pointer this
       function won&#39;t do anything.</b> See above for the restrictions as  to  when<br />       you are allowed to destroy the various types of bitmaps.</p><p>SEE ALSO<br />       create_bitmap(3), load_bitmap(3)
</p></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Sun, 15 Oct 2006 22:13:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Oops <img src="http://www.allegro.cc/forums/smileys/embarassed.gif" alt=":-[" /> When was it added? I recall having my game crash on stuff like that back in the dark 4.0.2 era. Thanks for the info though!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ron Ofir)</author>
		<pubDate>Sun, 15 Oct 2006 22:15:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Maybe it crashed freeing an unitialized BITMAP * that doesn&#39;t = NULL.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Sun, 15 Oct 2006 22:26:15 +0000</pubDate>
	</item>
</rss>
