<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Segmentation Fault while &quot;al_convert_mask_to_alpha&quot;.</title>
		<link>http://www.allegro.cc/forums/view/616012</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 29 Jan 2016 02:29:31 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hey.<br />I been trying to run a game I write under Windows on Debian Jessie. The Game is not truly finished but it should already display some basic mechanics. Game compiles and gets segmentation fault while running.</p><p>So what I know about the problem:</p><p>Program stops at first try of bitmap usage, while I&#39;m trying to convert it&#39;s mask to alpha, so probably i mess up something with bitmap loading.</p><p>The informations I get while running the game in gdb:
</p><div class="source-code snippet"><div class="inner"><pre>Program received <a href="http://www.delorie.com/djgpp/doc/libc/libc_724.html" target="_blank">signal</a> SIGSEGV, Segmentation fault.
<a href="http://www.allegro.cc/manual/al_lock_bitmap"><span class="a">al_lock_bitmap</span></a> <span class="k2">(</span>bitmap<span class="k3">=</span>bitmap@entry<span class="k3">=</span><span class="n">0x0</span>, format<span class="k3">=</span>format@entry<span class="k3">=</span><span class="n">0</span>, flags<span class="k3">=</span>flags@entry<span class="k3">=</span><span class="n">0</span><span class="k2">)</span>
   at <span class="k3">/</span>home<span class="k3">/</span>siery<span class="k3">/</span>Documents<span class="k3">/</span>lib<span class="k3">/</span>allegro-5.0.11<span class="k3">/</span>src<span class="k3">/</span>bitmap_lock.c:95
   <span class="n">95</span>         <span class="k1">return</span> <a href="http://www.allegro.cc/manual/al_lock_bitmap_region"><span class="a">al_lock_bitmap_region</span></a><span class="k2">(</span>bitmap, <span class="n">0</span>, <span class="n">0</span>, bitmap-&gt;w, bitmap-&gt;h, format, flags<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

So i enter backtrace command to get the current method and and value:</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#0  al_lock_bitmap (bitmap=bitmap@entry=0x0, format=format@entry=0, flags=flags@entry=0)</span>
    at <span class="k3">/</span>home<span class="k3">/</span>siery<span class="k3">/</span>Documents<span class="k3">/</span>lib<span class="k3">/</span>allegro-5.0.11<span class="k3">/</span>src<span class="k3">/</span>bitmap_lock.c:95
<span class="p">#1  0x00007ffff6eff7f9 in al_convert_mask_to_alpha (bitmap=0x0, mask_color=...)</span>
    at <span class="k3">/</span>home<span class="k3">/</span>siery<span class="k3">/</span>Documents<span class="k3">/</span>lib<span class="k3">/</span>allegro-5.0.11<span class="k3">/</span>src<span class="k3">/</span>bitmap.c:226
<span class="p">#2  0x0000000000402458 in main (argc=1, argv=0x7fffffffdd78) at precompiled/main.cpp:90</span>
</pre></div></div><p>

After examination i find out that 0x7ffff6eff7f9, so the function that crash, has value 0x0fc08548. That tells me absolute nothing.</p><p>In the code the initialization of bitmap seems to be good.</p><p>I declared it and then initialized as so:
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a> <span class="k3">*</span>ball_image <span class="k3">=</span> NULL<span class="k2">;</span>
ball_image <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_load_bitmap"><span class="a">al_load_bitmap</span></a><span class="k2">(</span> <span class="s">"../source/balls/ball01.png"</span> <span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

And then covert its background to alpha (thats line 90 of mine main.cpp file. There where gdb stops the program and declare segmentation fault).<br /><span class="source-code"><a href="http://www.allegro.cc/manual/al_convert_mask_to_alpha"><span class="a">al_convert_mask_to_alpha</span></a><span class="k2">(</span> ball_image, <a href="http://www.allegro.cc/manual/al_map_rgb"><span class="a">al_map_rgb</span></a><span class="k2">(</span><span class="n">255</span>, <span class="n">0</span>, <span class="n">255</span><span class="k2">)</span> <span class="k2">)</span><span class="k2">;</span></span></p><p>Only thing i change since moving the game from windows was location of graphics. But the path looks good. I attached main.cpp to this thread for review.</p><p>I&#39;m grateful for any response. Thanks, Siery.</p><p>Ps. I write this code over a year ago so i plan to rewrite a lot. Sorry if it looks chaotic <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (siery51)</author>
		<pubDate>Thu, 28 Jan 2016 03:38:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You aren&#39;t checking the return value of al_load_bitmap() for NULL (seems to be a lot of this recently...), it&#39;s probably failing to load the image.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bruce Pascoe)</author>
		<pubDate>Thu, 28 Jan 2016 07:48:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><pre>
&lt;code&gt;
// my source code here
int a = 1;
a++;
printf(&quot;%i\n&quot;, a);
&lt;/code&gt;
</pre><p>

...learn to use the code tags please.  With the above example, and code tags in place, it looks like...</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// my source code here</span>
<span class="k1">int</span> a <span class="k3">=</span> <span class="n">1</span><span class="k2">;</span>
a<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
<a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"%i\n"</span>, a<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

Which is much easier to read and you will get faster replies.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Roy)</author>
		<pubDate>Thu, 28 Jan 2016 09:14:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>@Bruce Pascoe: Yes, thats what I been thinking:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k2">(</span>gdb<span class="k2">)</span> x <a href="http://www.allegro.cc/manual/al_load_bitmap"><span class="a">al_load_bitmap</span></a><span class="k2">(</span> <span class="s">"../source/balls/ball01.png"</span> <span class="k2">)</span>
   <span class="n">0x0</span><span class="k2">:</span>    Cannot <a href="http://www.delorie.com/djgpp/doc/libc/libc_39.html" target="_blank">access</a> memory at address <span class="n">0x0</span>
</pre></div></div><p>

But I still can&#39;t see where I make fault. The path is correct. Here is full path I copy and past from terminal:<br /><span class="source-code">siery@sexxxy:~<span class="k3">/</span>Documents<span class="k3">/</span>code-base<span class="k3">/</span>cpp<span class="k3">/</span>super-pong<span class="k3">/</span>source<span class="k3">/</span>balls<span class="k3">/</span>ball01.png</span></p><p>You have any idea what can it be?</p><p>@Neil Roy: Sorry. Now I will use it since I know how you write it. I been angry as well writing code without marking <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (siery51)</author>
		<pubDate>Thu, 28 Jan 2016 11:13:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The only thing I can think of is that since you&#39;re using a relative path to load assets, your working directory at the time isn&#39;t what you think it is (tip: It&#39;s not always the directory containing your executable!)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bruce Pascoe)</author>
		<pubDate>Thu, 28 Jan 2016 11:19:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>In your eXamine call, you are using a relative address. Your working directory may be different than where your executable, and hence, your data is. How are you running the program?</p><p>Edit<br />Beaten.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Thu, 28 Jan 2016 11:32:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ow, thanks a lot! So i need to enter whole path in final version, as i can&#39;t run the program form other directory then where binary is?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (siery51)</author>
		<pubDate>Thu, 28 Jan 2016 11:36:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>No, don&#39;t hard-code absolute paths in your game, very bad idea!  Instead use <span class="source-code"><a href="http://www.allegro.cc/manual/al_get_standard_path"><span class="a">al_get_standard_path</span></a><span class="k2">(</span><span class="k2">)</span></span> to get your executable path and use Allegro&#39;s Path API to build an absolute path from your relative one.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bruce Pascoe)</author>
		<pubDate>Thu, 28 Jan 2016 12:43:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You can use the <a href="https://www.allegro.cc/manual/5/fshook.html">File system routines</a> to change the current working directory, <a href="https://www.allegro.cc/manual/5/al_get_standard_path">al_get_standard_path</a> to create a path to your executable, and the <a href="https://www.allegro.cc/manual/5/path.html">Path routines</a> to get a c string out of an ALLEGRO_PATH using <a href="https://www.allegro.cc/manual/5/al_path_cstr">al_path_cstr</a>.</p><div class="source-code snippet"><div class="inner"><pre><span class="c">/// Get the path to the executable</span>
ALLEGRO_PATH<span class="k3">*</span> exepath <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_get_standard_path"><span class="a">al_get_standard_path</span></a><span class="k2">(</span>ALLEGRO_EXENAME_PATH<span class="k2">)</span><span class="k2">;</span>

<span class="c">/// Remove the exe file name and replace it with an empty string</span>
<a href="http://www.allegro.cc/manual/al_set_path_filename"><span class="a">al_set_path_filename</span></a><span class="k2">(</span>exepath , NULL<span class="k2">)</span><span class="k2">;</span>

<span class="c">/// Change to the directory where the exe is</span>
<a href="http://www.allegro.cc/manual/al_change_directory"><span class="a">al_change_directory</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_path_cstr"><span class="a">al_path_cstr</span></a><span class="k2">(</span>exepath , ALLEGRO_NATIVE_PATH_SEP<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>

<span class="c">/// Clean up the path created by al_get_standard_path</span>
<a href="http://www.allegro.cc/manual/al_destroy_path"><span class="a">al_destroy_path</span></a><span class="k2">(</span>exepath<span class="k2">)</span><span class="k2">;</span>

<span class="c">/// Now load all files relative to the exe directory</span>
<a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a><span class="k3">*</span> monster_image <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_load_bitmap"><span class="a">al_load_bitmap</span></a><span class="k2">(</span><span class="s">"Images/Monster.png"</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Fri, 29 Jan 2016 02:29:31 +0000</pubDate>
	</item>
</rss>
