<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>video and system bitmaps crashing</title>
		<link>http://www.allegro.cc/forums/view/557972</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 08 Jan 2006 22:56:46 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hello,<br />I&#39;m having problems with my system crashing when I call destroy_bitmap() when the bitmap is a system or video bitmap, but not when it is a memory bitmap. This is roughly what I am doing, I&#39;m in 32bpp and my bitmaps are 32bpp, allegro is fully initialised and ready to go:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> someclass::LoadFunction<span class="k2">(</span><span class="k2">)</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> tmp<span class="k2">;</span>
 this-&gt;Image<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>filename,NULL<span class="k2">)</span><span class="k2">;</span>
 <span class="k1">if</span><span class="k2">(</span>WantSystemBitmap<span class="k2">)</span>
 <span class="k2">{</span>
  tmp<span class="k3">=</span><a href="http://www.allegro.cc/manual/create_system_bitmap" target="_blank"><span class="a">create_system_bitmap</span></a><span class="k2">(</span>Image-&gt;w,Image-&gt;h<span class="k2">)</span><span class="k2">;</span>
  <a href="http://www.allegro.cc/manual/blit" target="_blank"><span class="a">blit</span></a><span class="k2">(</span>Image,tmp,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,Image-&gt;w,Image-&gt;h<span class="k2">)</span><span class="k2">;</span>
  <span class="k1">if</span><span class="k2">(</span>tmp<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>Image<span class="k2">)</span><span class="k2">;</span>
    Image<span class="k3">=</span>tmp<span class="k2">;</span>
  <span class="k2">}</span>
 <span class="k2">}</span>
</pre></div></div><p>

Everything works fine and the graphic is displayed in the game.</p><p>4. On quitting I do this (allegro is still initialised, so no problems there).<br /><span class="source-code"><span class="k1">if</span><span class="k2">(</span>Image<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>Image<span class="k2">)</span><span class="k2">;</span></span></p><p>Anyone got a clue as to what could be causing it to crash? the bitmap definitely exists on the call to destroy and can still be viewed ok, it&#39;s memory address is the same as when it was initially created. It has no sub-bitmaps, etc. It only crashes when it is a system bitmap, so I guess it must be something to do with the creation of the bitmap, but I can&#39;t see a fault.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Walker)</author>
		<pubDate>Sat, 07 Jan 2006 05:53:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Your problem is not destroying them. You&#39;re setting Image to a deleted pointer. You need to do this:
</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">void</span> someclass::LoadFunction<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/BITMAP" target="_blank"><span class="a">BITMAP</span></a><span class="k3">*</span> tmp<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>filename,NULL<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">4</td><td> <span class="k1">if</span><span class="k2">(</span>tmp<span class="k2">)</span><span class="k2">{</span></td></tr><tr><td class="number">5</td><td>  <span class="k1">if</span><span class="k2">(</span>WantSystemBitmap<span class="k2">)</span></td></tr><tr><td class="number">6</td><td>  <span class="k2">{</span></td></tr><tr><td class="number">7</td><td>   this-&gt;Image<span class="k3">=</span><a href="http://www.allegro.cc/manual/create_system_bitmap" target="_blank"><span class="a">create_system_bitmap</span></a><span class="k2">(</span>tmp-&gt;w,tmp-&gt;h<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">8</td><td>   <a href="http://www.allegro.cc/manual/blit" target="_blank"><span class="a">blit</span></a><span class="k2">(</span>tmp,this-&gt;Image,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,Image-&gt;w,Image-&gt;h<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">9</td><td>   <a href="http://www.allegro.cc/manual/destroy_bitmap" target="_blank"><span class="a">destroy_bitmap</span></a><span class="k2">(</span>tmp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td>   <span class="k1">else</span></td></tr><tr><td class="number">11</td><td>   <span class="k2">{</span></td></tr><tr><td class="number">12</td><td>     this-&gt;Image<span class="k3">=</span><a href="http://www.allegro.cc/manual/create_bitmap" target="_blank"><span class="a">create_bitmap</span></a><span class="k2">(</span>tmp-&gt;w,tmp-&gt;h<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>     <a href="http://www.allegro.cc/manual/blit" target="_blank"><span class="a">blit</span></a><span class="k2">(</span>tmp,this-&gt;Image,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,Image-&gt;w,Image-&gt;h<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">14</td><td>     <a href="http://www.allegro.cc/manual/destroy_bitmap" target="_blank"><span class="a">destroy_bitmap</span></a><span class="k2">(</span>tmp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">15</td><td>   <span class="k2">}</span></td></tr><tr><td class="number">16</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">17</td><td> <span class="k2">}</span></td></tr></tbody></table></div></div><p>

That should do 100% better</p><p>EDIT: On second look, you&#39;re not setting it to a NULL pointer, but try this way anyways. It should work better.<br />EDIT AGAIN: Also, try calling release_bitmap(someclass-&gt;Image); brefore deleting it? Maybe? I dunno. That might help <b>shrugs</b>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Synapse Jumps)</author>
		<pubDate>Sat, 07 Jan 2006 14:29:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ll try it, but I don&#39;t see how it would work any better. </p><p>I&#39;m getting lots of wierd things happening. If I display the same bitmap but have enabled paging/triple buffering these graphics all point to the wrong images. It&#39;s as if creating my 2 or 3 video bitmaps for the screen buffers is affecting system/video bitmaps I create from the above method in my first post, in that instead of pointing to the correct image they are pointing to another image.</p><p>Neil.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Walker)</author>
		<pubDate>Sat, 07 Jan 2006 20:13:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
It&#39;s as if creating my 2 or 3 video bitmaps for the screen buffers is affecting system/video bitmaps I create from the above method in my first post, in that instead of pointing to the correct image they are pointing to another image.
</p></div></div><p>
Which order are you allocating things in? Of course some freaky stuff has to be done to support Allegro&#39;s &quot;unique&quot; screen update system in DirectX and there seems to be a lot going on that isn&#39;t documented in the normal readme files. But to directly copy a large chunk of text from the top of src/win/wddbmp.c:
</p><div class="quote_container"><div class="title">Allegro developers said:</div><div class="quote"><p>
The video bitmap allocation scheme works as follows:<br /> *  - the screen is allocated as a single DirectDraw surface (primary or overlay,<br /> *    depending on the driver) at startup,<br /> *  - the first video bitmap reuses the DirectDraw surface of the screen which is<br /> *    then assigned to flipping_page[0],<br /> *  - the second video bitmap allocates flipping_page[1] and uses it as its <br /> *    DirectDraw surface; it also destroys the single surface pointed to by<br /> *    flipping_page[0] and creates a DirectDraw flipping chain whose frontbuffer<br /> *    is connected back to flipping_page[0] and backbuffer to flipping_page[1],<br /> *  - the third video bitmap allocates flipping_page[2] and uses it as its <br /> *    DirectDraw surface; it also destroys the flipping chain pointed to by<br /> *    flipping_page[0] and flipping_page[1] and creates a new flipping chain<br /> *    whose frontbuffer is connected back to flipping_page[0], first backbuffer<br /> *    back to flipping_page[1] and second backbuffer to flipping_page[2].<br /> *<br /> * When a video bitmap is to be destroyed, the flipping chain (if it exists) is<br /> * destroyed and recreated with one less backbuffer, and its surfaces are assigned<br /> * back to the flipping_page[] array in order. If the video bitmap is not attached<br /> * to the surface that was just destroyed, its surface is assigned to the video<br /> * bitmap parent of the just destroyed surface.<br /> *<br /> * After triple buffering setup:<br /> *<br /> *  screen   video_bmp[0]  video_bmp[1]      video_bmp[2]<br /> *      \         |              |                |<br /> *       \        |              |                |<br /> *       page_flipping[0]  page_flipping[1]  page_flipping[2]<br /> *        (frontbuffer)-----(backbuffer1)-----(backbuffer2)<br /> *<br /> * After request_video_bitmap(video_bmp[1]):<br /> *<br /> *  screen   video_bmp[0]  video_bmp[1]      video_bmp[2]<br /> *      \                /                        |<br /> *       \               /\                        |<br /> *       page_flipping[0]  page_flipping[1]  page_flipping[2]<br /> *        (frontbuffer)-----(backbuffer1)-----(backbuffer2)<br /> *<br /> * This ensures that every video bitmap keeps track of the actual part of the<br /> * video memory it represents (see the documentation of DirectDrawSurface::Flip).
</p></div></div><p>
I&#39;m not 100% sure what to make of all this, but it definitely seems you should allocate your video surfaces that may become the frame buffer first...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Harte)</author>
		<pubDate>Sat, 07 Jan 2006 20:31:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I think (I&#39;ll look at the code tonight), but I&#39;m first creating 3 (or 2 for paged) video bitmaps to cover the screen bitmaps, then I&#39;m creating the smaller bitmaps from the memory bitmaps.</p><p>Thomas, was my code right or should I be doing it the way Synaps Jump recommended, as I can&#39;t see anything wrong with my code.</p><p>I should say, that when I&#39;m getting errors with the video/system bitmaps seemingly looking at the wrong bitmap (though memory bitmaps work), only happens when I&#39;m using sub-bitmaps of video or system bitmaps.</p><p>I can let you see my code if you wish, but it&#39;s spread over a couple of files <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>One final nail in my video/system bitmap is when I try and delete my surface bitmap, e.g. destroy_bitmap(page[0]) it crashes with a pointer error, but I&#39;ve checked and I haven&#39;t got the screen locked for drawing and just a few seconds before this my game loop was showing the video pages fine.</p><p>Now, another problem I&#39;m having that I noticed on a very simple example that just used textprintf_ex() was there was lots of white noise in blocks over my screen when I use video bitmaps for the surface. I&#39;m simply using the code as shown in the allegro help file.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Walker)</author>
		<pubDate>Sat, 07 Jan 2006 22:24:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I can&#39;t see any real difference in validity between the two snippets of code in this thread. I have vague memories of receiving crashes on destroy_bitmap for video bitmaps when I felt I shouldn&#39;t many years ago, but I don&#39;t think Allegro.cc was around at the time and I don&#39;t think I submited a bug report. OS X Allegro doesn&#39;t support video or system bitmaps so I&#39;ve had no real experience lately.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Harte)</author>
		<pubDate>Sat, 07 Jan 2006 22:36:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Thomas Harte said:</div><div class="quote"><p>

I have vague memories of receiving crashes on destroy_bitmap for video bitmaps when I felt I shouldn&#39;t many years ago
</p></div></div><p>
Was this perchance related to using the Allegro mousecursor and not calling show_mouse(NULL) before destroying the bitmaps? Only now (post 4.2.0) has it been mentioned in the docs.</p><p>AE.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Andrei Ellman)</author>
		<pubDate>Sun, 08 Jan 2006 00:56:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Fixed a couple of things! which I guess might serve as something to remember for others!</p><p>1. my system was crashing at the end because I was calling set_gfx_mode(GFX_TEXT...) before I deleted my video bitmaps</p><p>2. I was experiencing white noise because I&#39;d forgotton to clear the bitmaps prior to using them (memory were ok)</p><p>3. I was sometimes not seeing text because it was written to the screen object and I guess when you use paging/triple you shouldn&#39;t be doing this.</p><p>As for the code Thomas mentioned:
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
</p><ul><li><p>- the first video bitmap reuses the DirectDraw surface of the screen which is then assigned to flipping_page[0],
</p></li><li><p>When a video bitmap is to be destroyed, the flipping chain (if it exists) is
</p></li><li><p>destroyed
</p></li></ul></div></div><p>

I don&#39;t understand this, as it sounds like video_bitmaps are only used for screen buffers and are allocated to flipping_page[x]. What about normal bitmaps that you want to simply store in video memory?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Walker)</author>
		<pubDate>Sun, 08 Jan 2006 05:47:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
1. my system was crashing at the end because I was calling set_gfx_mode(GFX_TEXT...) before I deleted my video bitmaps
</p></div></div><p>
Indeed!
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
1.10.7 create_video_bitmap<br />--------------------------<br />[...]<br />     Returns a pointer to the bitmap on success, or NULL if you have<br />     run out of video ram. <b>Remember to destroy this bitmap before any
     subsequent call to set_gfx_mode().</b>
</p></div></div><p>

</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
2. I was experiencing white noise because I&#39;d forgotton to clear the bitmaps prior to using them (memory were ok)
</p></div></div><p>
In that case, your malloc() clears the returned memory. This isn&#39;t guarenteed and you can&#39;t count on it, even for memory bitmaps. Always clear bitmaps you just allocated.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
3. I was sometimes not seeing text because it was written to the screen object and I guess when you use paging/triple you shouldn&#39;t be doing this.
</p></div></div><p>
Correct. You draw onto the pages you allocate. This is mentioned in the docs, but I can understand if you missed it:
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
1.10.7 create_video_bitmap<br />--------------------------</p><p>BITMAP *create_video_bitmap(int width, int height);<br />     Allocates a video memory bitmap of the specified size. This can be<br />     used to allocate offscreen video memory for storing source<br />     graphics ready for a hardware accelerated blitting operation, or<br />     to create multiple video memory pages which can then be displayed<br />     by calling show_video_bitmap().  Read the introduction of this<br />     chapter for a comparison with other types of bitmaps and other<br />     specific details.</p><p>     <b>Warning: video memory bitmaps are usually allocated from the same
     space as the screen bitmap, so they may overlap with it; it is
     therefore not a good idea to use the global screen at the same
     time as any surfaces returned by this function.</b>
</p></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Sun, 08 Jan 2006 06:00:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The reason for all these crap coding errors was that my framework is a large beast now and triple/paging was put in at the end so I had to go through all my code and check whether all the stuff that accessed the current buffer was valid code and/or had to change, and as this is the first time I&#39;ve used system/video bitmaps and paging/triple buffering it took a few re-reads of the code to find them all as the code by itself looks ok, just in the context of video buffers isn&#39;t.</p><p>But yes, I did read those bits, they are the reason why I fixed my code. God bless the Allegro manual <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>I&#39;ve only my mysterious switching sub-bitmaps when using video/system memory as the parent to look at now <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Walker)</author>
		<pubDate>Sun, 08 Jan 2006 06:18:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, what I sortof meant to say was, if you think there are things that can be improved in the manual to make some of these caveats more obvious, feel free to say so.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Sun, 08 Jan 2006 15:52:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Not really, the manual is a reference guide and serves it&#39;s purpose brilliantly.</p><p>Maybe if someone wrote a programming guide to go with it, that would be a different story, as that would tie the stuff together.</p><p>Neil.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Walker)</author>
		<pubDate>Sun, 08 Jan 2006 22:56:46 +0000</pubDate>
	</item>
</rss>
