|
Why is this code crashing??? |
Battyhal
Member #15,775
October 2014
|
Hi everybody !! 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. I received the "Segmentation fault" advice in the GetX() method which is necessary to check for a collision . If i comment it, the error moves to GetY() method. 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. Thanks a lot in advance and happy new year!!. |
Chris Katko
Member #1,881
January 2002
|
At first glance, if you're blowing up on a simple get routine, it probably means you'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're all pointing nowhere. Either you're: Actually, come to think of it, you shouldn'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're talking to a base class or an inherited version at run-time. Also, here: 1if((*iterEnemigos)->GetestaVivo())
2 {
3 if((*iterBalas)->colision(iterBalas, iterEnemigos))
4 {
5 delete(*iterBalas);
6 iterBalas = balas.erase(iterBalas);
7 PROYECTILs_destruidas++;
8
9 delete(*iterEnemigos);
10 iterEnemigos = enemigos.erase(iterEnemigos);
11 asteroides_destruidos++;
12 nave->puntos++;
13 al_play_sample(boom, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, 0);
14 if(!boom)
15 cout << "NO BOOM!";
16 cout << "HE llegado";
17 }
18 }
19
20 else if((*iterBalas)->fuera(iterBalas, ALTURA_DISPLAY, ANCHURA_DISPLAY))
21 {
22 delete (*iterBalas);
23 iterBalas = balas.erase(iterBalas);
24 PROYECTILs_destruidas++;
25 }
It looks like you are deleting the object pointed to by iterEnemigos and iterBalas but immediately after that in the else if, you're dereferencing ("following") the pointer (that you destroyed) to nowhere, and then once at the nowhere you're trying to call methods on it which blow up. That's not necessarily the GetX seg fault though. -----sig: |
Battyhal
Member #15,775
October 2014
|
Hi Chris !! I have to apologize for my delay in answer your reply... Been busy with christmas presents and celebrations. 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 "bullets" and "enemies") . But i suppose is not always the best implementation isn´t it ?. 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 ?. Thanks very much for your time !! And happy New Year !! |
|