<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Access Violation in al_flip_display with threads</title>
		<link>http://www.allegro.cc/forums/view/617754</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 21 Feb 2019 18:29:57 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><s>I have started programming a multithreaded game, which has 3 threads:
- Drawing
- Updating
- Main (started the other two, event handling etc.)

And my problem occurs in the drawing thread:</s>
</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">  1</span><span class="k1">void</span> <span class="k3">*</span>windowThread<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_THREAD"><span class="a">ALLEGRO_THREAD</span></a> <span class="k3">*</span>thread, <span class="k1">void</span><span class="k3">*</span> arg<span class="k2">)</span> <span class="k2">{</span>
<span class="number">  2</span>   <span class="k1">void</span> <span class="k3">*</span><span class="k3">*</span>arguments <span class="k3">=</span> <span class="k2">(</span><span class="k1">void</span><span class="k3">*</span><span class="k3">*</span><span class="k2">)</span>arg<span class="k2">;</span>
<span class="number">  3</span>   <a href="http://www.allegro.cc/manual/ALLEGRO_DISPLAY"><span class="a">ALLEGRO_DISPLAY</span></a> <span class="k3">*</span>display<span class="k2">;</span>
<span class="number">  4</span>
<span class="number">  5</span>   DrawList <span class="k3">*</span>drawList <span class="k3">=</span> <span class="k2">(</span><span class="k2">(</span>DrawList<span class="k3">*</span><span class="k2">)</span>arguments<span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  6</span>   <a href="http://www.allegro.cc/manual/ALLEGRO_EVENT_QUEUE"><span class="a">ALLEGRO_EVENT_QUEUE</span></a> <span class="k3">*</span>eventQueue <span class="k3">=</span> <span class="k2">(</span><span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_EVENT_QUEUE"><span class="a">ALLEGRO_EVENT_QUEUE</span></a><span class="k3">*</span><span class="k2">)</span>arguments<span class="k2">[</span><span class="n">1</span><span class="k2">]</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  7</span>
<span class="number">  8</span>   display <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_display"><span class="a">al_create_display</span></a><span class="k2">(</span><span class="n">1280</span>, <span class="n">720</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  9</span>   <a href="http://www.allegro.cc/manual/al_register_event_source"><span class="a">al_register_event_source</span></a><span class="k2">(</span>eventQueue, <a href="http://www.allegro.cc/manual/al_get_display_event_source"><span class="a">al_get_display_event_source</span></a><span class="k2">(</span>display<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 10</span>   
<span class="number"> 11</span>   <span class="k1">while</span> <span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/al_get_thread_should_stop"><span class="a">al_get_thread_should_stop</span></a><span class="k2">(</span>thread<span class="k2">)</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 12</span>      drawlist::List <span class="k3">*</span>drawbuffer <span class="k3">=</span> drawList-&gt;getScreenList<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 13</span>      <span class="k1">while</span><span class="k2">(</span>drawbuffer-&gt;consume<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 14</span>        <span class="c">//...Draw Stuff...</span>
<span class="number"> 15</span>      <span class="k2">}</span>
<span class="number"> 16</span>      drawList-&gt;ready<span class="k2">(</span>drawbuffer<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 17</span>      <a href="http://www.allegro.cc/manual/al_flip_display"><span class="a">al_flip_display</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 18</span>    <span class="k2">}</span>
<span class="number"> 19</span>
<span class="number"> 20</span>    <span class="k1">return</span> nullptr<span class="k2">;</span>
<span class="number"> 21</span>  <span class="k2">}</span>
</div></div><p>
<s>
<tt>drawList</tt> just keeps two pointers to <tt>drawlist::Lists</tt>, one for the update thread to be filled, and one for the drawing thread to be drawn and upon calling <tt>drawList-&gt;ready()</tt> it blocks with an <tt>ALLEGRO_COND</tt> until the other thread has also called ready, then the two pointers are swapped so that upon the next call of <tt>getScreenList()</tt> it returns the other list, that was previously filled in by the update thread.

And now the problem:
In <tt>al_flip_display()</tt> (display.c 190) the call to <tt>al_get_current_display()</tt> fails, because the <tt>threadlocalstate</tt> from which it gets the display (tlc.c 396) is &quot;0x323F800000&quot; which causes an access violation.
</s></p><p>Total suprise: The problem wasn&#39;t actually caused by allegro but instead by some other thing which had a buffer overflow.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (schollexpos)</author>
		<pubDate>Thu, 21 Feb 2019 16:39:28 +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/617754/1041384#target">schollexpos</a> said:</div><div class="quote"><p> Total suprise: The problem wasn&#39;t actually caused by allegro but instead by some other thing which had a buffer overflow. </p></div></div><p>Ah, the best kind of bug. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>I remember many nights spent debugging those sort of errors...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Erin Maus)</author>
		<pubDate>Thu, 21 Feb 2019 17:15:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You&#39;ll want to be careful using a display in another thread like that. There are certain things you need to be aware of.</p><p>You will only get hw accelerated drawing on bitmaps created, or converted on that thread when that display is current. So if you load or create bitmaps in another thread they must be converted to the proper format before you get acceleration.</p><p>You can make sure they are loaded in the right format by 1) loading them in the same thread as the one that created the display, or 2) by cheating and setting the target bitmap to the backbuffer of the display created on that thread. That would make that thread and display current for loading and creation purposes. You would need to un set the current display for that thread when you were done then, and re-set it for the display thread.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Thu, 21 Feb 2019 18:29:57 +0000</pubDate>
	</item>
</rss>
