<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Why is this code crashing??? </title>
		<link>http://www.allegro.cc/forums/view/615957</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Mon, 04 Jan 2016 00:01:04 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hi everybody !!</p><p>I´m in the process of learning C++ and Allegro 5.0. I´m following a great on-line  tutorial of Mike Geig, but i´m stacked in a problem with the code for a very simple space shooter program. <br />I have a PROJECTILE class with some &quot;classic&quot; methods, like &quot;Getters&quot;, &quot;Setters&quot; and in one of the Get´s methods i received always a crash and i do not know why. Attached are the header and implementation files for the PROJECTILE class (in spanish is PROYECTIL) and the main file.</p><p>I received the &quot;Segmentation fault&quot; advice in the GetX() method which is necessary to check for a collision . If i comment it, the error moves to GetY() method. </p><p>If someone could take a look at it and give some enlightenment i would be very thankful because i do not really know how to bypass this error.</p><p>Thanks a lot in advance and happy new year!!.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Battyhal)</author>
		<pubDate>Wed, 30 Dec 2015 19:18:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>At first glance, if you&#39;re blowing up on a simple get routine, it probably means you&#39;re using pointers but you messed up the pointer somewhere and it points to an invalid place. That means regardless of the method you call, it should explode because they&#39;re all pointing nowhere.</p><p>Either you&#39;re:<br /> - adding invalid ones onto the STL list<br /> - breaking the list somewhere<br /> - Incorrectly keeping the number of elements on the list and when you delete some, you&#39;re going past the end.<br /> - You&#39;re keeping a pointer to something that has been destroyed and then accessing it.</p><p>Actually, come to think of it, you shouldn&#39;t necessarily need to use pointers AND an STL list. If I recall correctly, you only need pointers AND STL lists when you use inheritance so you can tell whether you&#39;re talking to a base class or an inherited version at run-time.</p><p>Also, here:
</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">if</span><span class="k2">(</span><span class="k2">(</span><span class="k3">*</span>iterEnemigos<span class="k2">)</span><span class="k3">-</span><span class="k3">&gt;</span>GetestaVivo<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span>
<span class="number">  2</span>                            <span class="k2">{</span>
<span class="number">  3</span>                                <span class="k1">if</span><span class="k2">(</span><span class="k2">(</span><span class="k3">*</span>iterBalas<span class="k2">)</span><span class="k3">-</span><span class="k3">&gt;</span>colision<span class="k2">(</span>iterBalas, iterEnemigos<span class="k2">)</span><span class="k2">)</span>
<span class="number">  4</span>                                <span class="k2">{</span>
<span class="number">  5</span>                                    <span class="k1">delete</span><span class="k2">(</span><span class="k3">*</span>iterBalas<span class="k2">)</span><span class="k2">;</span>
<span class="number">  6</span>                                    iterBalas <span class="k3">=</span> balas.erase<span class="k2">(</span>iterBalas<span class="k2">)</span><span class="k2">;</span>
<span class="number">  7</span>                                    PROYECTILs_destruidas<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
<span class="number">  8</span>
<span class="number">  9</span>                                    <span class="k1">delete</span><span class="k2">(</span><span class="k3">*</span>iterEnemigos<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 10</span>                                    iterEnemigos <span class="k3">=</span> enemigos.erase<span class="k2">(</span>iterEnemigos<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 11</span>                                    asteroides_destruidos<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
<span class="number"> 12</span>                                    nave-&gt;puntos<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
<span class="number"> 13</span>                                    <a href="http://www.allegro.cc/manual/al_play_sample"><span class="a">al_play_sample</span></a><span class="k2">(</span>boom, <span class="n">1</span>, <span class="n">0</span>, <span class="n">1</span>, ALLEGRO_PLAYMODE_ONCE, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 14</span>                                    <span class="k1">if</span><span class="k2">(</span><span class="k3">!</span>boom<span class="k2">)</span>
<span class="number"> 15</span>                                        cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"NO BOOM!"</span><span class="k2">;</span>
<span class="number"> 16</span>                                    cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"HE llegado"</span><span class="k2">;</span>
<span class="number"> 17</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">else</span> <span class="k1">if</span><span class="k2">(</span><span class="k2">(</span><span class="k3">*</span>iterBalas<span class="k2">)</span><span class="k3">-</span><span class="k3">&gt;</span>fuera<span class="k2">(</span>iterBalas, ALTURA_DISPLAY, ANCHURA_DISPLAY<span class="k2">)</span><span class="k2">)</span>
<span class="number"> 21</span>                            <span class="k2">{</span>
<span class="number"> 22</span>                                <span class="k1">delete</span> <span class="k2">(</span><span class="k3">*</span>iterBalas<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 23</span>                                iterBalas <span class="k3">=</span> balas.erase<span class="k2">(</span>iterBalas<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 24</span>                                PROYECTILs_destruidas<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
<span class="number"> 25</span>                            <span class="k2">}</span>
</div></div><p>

It looks like you are deleting the object pointed to by iterEnemigos and iterBalas but immediately after that in the else if, you&#39;re dereferencing (&quot;following&quot;) the pointer (that you destroyed) to nowhere, and then once at the nowhere you&#39;re trying to call methods on it which blow up.</p><p>That&#39;s not necessarily the GetX seg fault though.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Thu, 31 Dec 2015 01:55:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
Hi Chris !!</p><p>I have to apologize for my delay in answer your reply... Been busy with christmas presents and celebrations.</p><p>And about your answer: i have used pointers and an STL container because i thought it was the most efficient way to store user-defined types (like this &quot;bullets&quot; and &quot;enemies&quot;) . But i suppose is not always the best implementation isn´t it ?. </p><p>And in the code i first check if the enemy is alive (is inside the screen and not hit by a bullet) and if it is, i check for a collision and if there is a collision i delete it. If not i check if it is outside the screen and if that is the case i delete it. Do you think that logic is not correct? Should i rewrite it from scratch?. And in that case could you please give me some advice about how to do it ?. </p><p>Thanks very much for your time !! And happy New Year !! <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Battyhal)</author>
		<pubDate>Mon, 04 Jan 2016 00:01:04 +0000</pubDate>
	</item>
</rss>
