<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Question About &#39;destroy_bitmap&#39;</title>
		<link>http://www.allegro.cc/forums/view/569034</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 25 Feb 2006 15:42:51 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I understand that if you try to use free() on a location that has &#39;already&#39; been freed, this can lead to memory leaks. However, I&#39;m curious to know if the same applies to Allegro&#39;s destroy_bitmap() function. Is it safe for my program to run that function on a location that is not allocated?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Damian Grove)</author>
		<pubDate>Fri, 24 Feb 2006 11:22:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>No, it&#39;s not safe.</p><p>The problem with double free() is not with memory leaking, but crashes.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Wang)</author>
		<pubDate>Fri, 24 Feb 2006 11:37:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Best case in a well-behaving environment double-freeing doesn&#39;t cause memory leaks - it&#39;s FATAL.<br />Worst case in a misbehaving environment, double-freeing is &quot;undefined behavior&quot;, which can be even worse than fatal.</p><p>destroy_bitmap() involves a free() operation, so, you do the math  <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Birdeeoh)</author>
		<pubDate>Fri, 24 Feb 2006 12:49:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I think calling free/destroy_bitmap on null pointers is safe. That means, every time you free some memory or delete a bitmap, set it&#39;s pointer to null.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Fri, 24 Feb 2006 14:28:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>if the pointer is null then destroy_bitmap returns without doing anything. this then means if you have an unitialised pointer variable with some arbitrary random number then call destroy_bitmap will do bad things. Snippet from destroy_bitmap:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> <a href="http://www.allegro.cc/manual/destroy_bitmap" target="_blank"><span class="a">destroy_bitmap</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span>bitmap<span class="k2">)</span>
<span class="k2">{</span>
   <span class="k1">if</span> <span class="k2">(</span>bitmap<span class="k2">)</span> <span class="k2">{</span>
      ...stuff
      <a href="http://www.delorie.com/djgpp/doc/libc/libc_350.html" target="_blank">free</a><span class="k2">(</span>bitmap<span class="k2">)</span><span class="k2">;</span>
   <span class="k2">}</span>
<span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Walker)</author>
		<pubDate>Fri, 24 Feb 2006 18:59:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes - but there&#39;s a big difference between a NULL pointer -
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a><span class="k3">*</span> bmp <span class="k3">=</span> <span class="n">0</span><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> bmp <span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
and an already freed ptr -
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a><span class="k3">*</span> bmp <span class="k3">=</span> <a href="http://www.allegro.cc/manual/load_bitmap" target="_blank"><span class="a">load_bitmap</span></a><span class="k2">(</span> <span class="s">"somebitmap.bmp"</span> <span class="k2">)</span><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> bmp <span class="k2">)</span><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> bmp <span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
Doing this will crash yuh.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Birdeeoh)</author>
		<pubDate>Fri, 24 Feb 2006 21:54:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes, that&#39;s what I said. If you free a pointer, it doesn&#39;t automatically give itself a value of 0. If you code properly, as in ensuring your pointers are either valid or null:</p><p>BITMAP* bmp=NULL;<br />...<br />...<br />destroy_bitmap(bmp);<br />bmp=NULL;</p><p>Then all will be well.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Walker)</author>
		<pubDate>Fri, 24 Feb 2006 22:05:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Does &quot;delete&quot; have the same behaviour? I use it without any checks:o
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Murat AYIK)</author>
		<pubDate>Fri, 24 Feb 2006 23:08:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>delete has the same behaviour. Always set them to NULL after you delete them, if you plan to use them again.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Derezo)</author>
		<pubDate>Sat, 25 Feb 2006 00:56:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If I remember correctly then NULL has been deprecated and regular 0 should be used instead. I don&#39;t remember if this is only in C++ or in C too though.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Sat, 25 Feb 2006 02:44:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">ISO/IEC 14882:1998, Section 18.1.4 said:</div><div class="quote"><p>

The macro NULL is an implementationdefined<br />C++ null pointer constant in this International Standard<br />(4.10). Possible definitions include 0 and 0L, but not (void*)0.
</p></div></div><p>

Not the newest source, I admit.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Indeterminatus)</author>
		<pubDate>Sat, 25 Feb 2006 03:07:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
If I remember correctly then NULL has been deprecated
</p></div></div><p>Then why did you use it to begin with anyway? <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" /> <br />See your first post <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><p>MSVC 8 complains about a lot of things being deprecated. itoa(), sprintf(), _snprintf(), and more. It does not complain about NULL. If it is deprecated, google doesn&#39;t favour the pages that explain it.</p><p>In C, 0 is not NULL. In C++, NULL is defined as 0. It just makes more sense to use NULL (I want a null pointer, not zero).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Derezo)</author>
		<pubDate>Sat, 25 Feb 2006 04:36:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Then why did you use it to begin with anyway?
</p></div></div><p>How to you spell &quot;0&quot;? I know it can be written as &quot;zero&quot; or &quot;null&quot;, possibly something else too <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
MSVC 8 complains about a lot of things being deprecated.
</p></div></div><p>As I&#39;ve heard it does. As it doesn&#39;t like standards a lot then I&#39;m not suprised it doesn&#39;t do anything about the NULL macro. Though I&#39;m not sure if any other compilers do.
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
In C, 0 is not NULL.
</p></div></div><p>But it has to be something that evaluates to 0. If it doesn&#39;t it might be a bit difficult to use in conditionals like <i>if (blah) bleh();</i>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Sat, 25 Feb 2006 05:00:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I found this in MinGW/stddef.h:<br /><i>#define NULL __null</i></p><p>I tried adding this to my prog:<br /><i>#define __null 0</i></p><p>It doesn&#39;t complain but this is also weird. Then what is &quot;__null&quot;? (or what was:))
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Murat AYIK)</author>
		<pubDate>Sat, 25 Feb 2006 05:08:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I believe it is (void*)0 in <i>C</i>. Could be wrong.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
How to you spell &quot;0&quot;? I know it can be written as &quot;zero&quot; or &quot;null&quot;, possibly something else too <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div></div><p>It&#39;s a case sensitive argument? <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Derezo)</author>
		<pubDate>Sat, 25 Feb 2006 05:10:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Probably some kind of compiler extension.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Milan Mimica)</author>
		<pubDate>Sat, 25 Feb 2006 05:23:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Why can&#39;t I get this thing to crash?!</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="p">#define msj allegro_message</span></td></tr><tr><td class="number">2</td><td><span class="p">#include &lt;allegro.h&gt;</span></td></tr><tr><td class="number">3</td><td>&#160;</td></tr><tr><td class="number">4</td><td><span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">5</td><td><span class="k2">{</span></td></tr><tr><td class="number">6</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> <span class="c">//set_gfx_mode(GFX_TEXT, 0,0,0,0);</span></td></tr><tr><td class="number">7</td><td>  </td></tr><tr><td class="number">8</td><td>  <span class="k1">int</span> <span class="k3">*</span>ptr<span class="k2">;</span></td></tr><tr><td class="number">9</td><td>  </td></tr><tr><td class="number">10</td><td>  msj<span class="k2">(</span><span class="s">"newing"</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">11</td><td>  ptr <span class="k3">=</span> <span class="k1">new</span> <span class="k1">int</span><span class="k2">;</span><span class="c">//[5];</span></td></tr><tr><td class="number">12</td><td>  msj<span class="k2">(</span><span class="s">"newed\n%d"</span>, ptr<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>  </td></tr><tr><td class="number">14</td><td>  <span class="k1">delete</span><span class="k2">[</span><span class="k2">]</span> ptr<span class="k2">;</span> <span class="c">//delete ptr;</span></td></tr><tr><td class="number">15</td><td>  msj<span class="k2">(</span><span class="s">"deleted\n%d"</span>, ptr<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>&#160;</td></tr><tr><td class="number">17</td><td>  <span class="k1">delete</span><span class="k2">[</span><span class="k2">]</span> ptr<span class="k2">;</span> <span class="c">//delete ptr;</span></td></tr><tr><td class="number">18</td><td>  msj<span class="k2">(</span><span class="s">"deleted again\n%d"</span>, ptr<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">19</td><td>&#160;</td></tr><tr><td class="number">20</td><td>  ptr <span class="k3">=</span> NULL<span class="k2">;</span></td></tr><tr><td class="number">21</td><td>  msj<span class="k2">(</span><span class="s">"ptr/NULL = %d"</span>, ptr<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">22</td><td>  </td></tr><tr><td class="number">23</td><td>  ptr <span class="k3">+</span><span class="k3">=</span> <span class="n">2</span><span class="k2">;</span></td></tr><tr><td class="number">24</td><td>  msj<span class="k2">(</span><span class="s">"ptr_int +2 = %d"</span>, ptr<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">25</td><td>&#160;</td></tr><tr><td class="number">26</td><td>  <span class="k1">return</span> <span class="n">1</span><span class="k2">;</span></td></tr><tr><td class="number">27</td><td><span class="k2">}</span> <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>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Murat AYIK)</author>
		<pubDate>Sat, 25 Feb 2006 05:24:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>`Proper&#39; programming practice (according to some) says that in C++, one should use 0 instead of NULL also for pointers (don&#39;t ask me why).<br />In C, a pointer that is NULL evaluates to the boolean value 0 and an integer of 0 assigned to a pointer will set the pointer to NULL. That said, wether or not NULL is actually 0, (void *)0 or something else entirely is platform dependent.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Sat, 25 Feb 2006 05:29:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Why can&#39;t I get this thing to crash?!
</p></div></div><p>
Because you&#39;re relying on undefined behavior. Undefined behavior may mean that your program will work as expected just as well as making your computer burst into flames. You don&#39;t know which (or if there is anything in between), and the results don&#39;t need to be consistent from run to run.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bob)</author>
		<pubDate>Sat, 25 Feb 2006 07:42:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Murat AYIK:</p><p>As Bob already pointed out, &quot;undefined behavior&quot; is just that - undefined.  Early when I said &quot;IT IS FATAL&quot; or something like that, my point was twofold.  A - it doesn&#39;t cause memory leaks as originally claimed, but rather the error would be a fatal fault  and B - say something is &quot;FATAL&quot; usually encourages people not to do it  <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>It PROBABLY won&#39;t help you get a crash, but also try 
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span><span class="k3">*</span> data <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_551.html" target="_blank">malloc</a><span class="k2">(</span> <span class="n">1024</span> <span class="k2">)</span><span class="k2">;</span>
<a href="http://www.delorie.com/djgpp/doc/libc/libc_350.html" target="_blank">free</a><span class="k2">(</span> data <span class="k2">)</span><span class="k2">;</span>
<a href="http://www.delorie.com/djgpp/doc/libc/libc_350.html" target="_blank">free</a><span class="k2">(</span> data <span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
if you&#39;re trying to demonstrate this bad behavior.  But it&#39;s also quite possible it won&#39;t bite you until you accidentally do it in a complex program  <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Birdeeoh)</author>
		<pubDate>Sat, 25 Feb 2006 12:20:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>OK, I just wanted to see what would happen during the dying process:) </p><p>Also neither new nor the malloc example crashed in MinGW or Turbo C++ 3.0 . Damn compilers! They don&#39;t produce miserable executables when you need one:P</p><p>So using zero or a #define trick would be safe, right?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Murat AYIK)</author>
		<pubDate>Sat, 25 Feb 2006 15:14:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Destroying a previously freed bitmap will probably end in segmentation fault, definately under Win2k and XP. This should work:
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/DATAFILE" target="_blank"><span class="a">DATAFILE</span></a> <span class="k3">*</span>gfx<span class="k2">;</span>
<a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span>bmp<span class="k2">;</span>

gfx <span class="k3">=</span> <a href="http://www.allegro.cc/manual/load_datafile" target="_blank"><span class="a">load_datafile</span></a><span class="k2">(</span><span class="s">"blah.dat"</span><span class="k2">)</span><span class="k2">;</span>
bmp <span class="k3">=</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span><span class="k2">)</span>gfx<span class="k2">[</span>OBJECT<span class="k2">]</span>.dat<span class="k2">;</span>

<a href="http://www.allegro.cc/manual/unload_datafile" target="_blank"><span class="a">unload_datafile</span></a><span class="k2">(</span>gfx<span class="k2">)</span><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>bmp<span class="k2">)</span><span class="k2">;</span>  <span class="c">// at this point it will die by horrible death</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (OICW)</author>
		<pubDate>Sat, 25 Feb 2006 15:42:51 +0000</pubDate>
	</item>
</rss>
