Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » Segmantation fault

This thread is locked; no one can reply to it. rss feed Print
Segmantation fault
Jawall
Member #15,162
June 2013

There is something in my program that not good because when i run it there is the Segmentation Fault...

This is the code:

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_image.h> 3#include "stars.h" 4 5enum Stato_t {START,GAME,GAMEOVER} stato; 6enum COLOR{VERDE,GIALLO,VIOLA,ARANCIONE,ROSSO,BLU}; 7 8const int MAT_SIZE = 3; 9 10Star star[MAT_SIZE * MAT_SIZE]; 11 12void InitStar(void); 13void DrawCell(ALLEGRO_BITMAP *cella, int startx, int starty); 14 15int main(void) 16{ 17 //variables 18 int width = 640; 19 int height = 640; 20 bool done = false; 21 bool render = false; 22 int startx = 10, starty = 10; 23 24 //allegro variable 25 ALLEGRO_DISPLAY *display = NULL; 26 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 27 ALLEGRO_TIMER *timer; 28 ALLEGRO_BITMAP *image,*cella; 29 30 //program init 31 if(!al_init()) //initialize Allegro 32 return -1; 33 34 display = al_create_display(width, height); //create our display object 35 36 if(!display) //test display object 37 return -1; 38 39 //addon init 40 al_install_keyboard(); 41 al_install_mouse(); 42 al_init_image_addon(); 43 44 image = al_load_bitmap("candy.bmp"); 45 al_convert_mask_to_alpha(image, al_map_rgb(106, 76, 48)); 46 47 cella = al_load_bitmap("cella.bmp"); 48 al_convert_mask_to_alpha(cella, al_map_rgb(106, 76, 48)); 49 50 event_queue = al_create_event_queue(); 51 timer = al_create_timer(1.0 / 60); 52 53 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 54 al_register_event_source(event_queue, al_get_keyboard_event_source()); 55 al_register_event_source(event_queue, al_get_mouse_event_source()); 56 57 //InitStar(); 58 59 al_start_timer(timer); 60 61 while(!done) 62 { 63 ALLEGRO_EVENT ev; 64 al_wait_for_event(event_queue, &ev); 65 66 if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 67 { 68 if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) 69 { 70 71 done = true; 72 } 73 } 74 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {} 75 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {} 76 else if(ev.type == ALLEGRO_EVENT_TIMER) 77 { 78 render = true; 79 } 80 81 //if(render && al_is_event_queue_empty(event_queue)) { 82 // render = false; 83 DrawCell(cella,startx,starty); 84 //} 85 86 al_flip_display(); 87 al_clear_to_color(al_map_rgb(0,0,0)); 88 } 89 90 91 al_destroy_event_queue(event_queue); 92 al_destroy_display(display); //destroy our display object 93 al_destroy_bitmap(image); 94 al_destroy_bitmap(cella); 95 96 return 0; 97} 98 99 100void InitStar() {} 101 102void DrawCell(ALLEGRO_BITMAP *cella,int startx, int starty) { 103 104 for(int j=0; j<MAT_SIZE; j++) 105 for(int i=0; i<MAT_SIZE; i++) 106 al_draw_bitmap(cella,i*al_get_bitmap_width(cella)+startx,j*al_get_bitmap_height(cella)+starty,0); 107}

Please, help me!

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Your 'image' or 'cella' bitmaps are likely failing to load properly. Check the return value of al_load_bitmap and other functions that might fail.

If it is not loading them correctly check that they are in the same directory as your executable and that your IDE is running the program from that same directory.

Jawall
Member #15,162
June 2013

I have checked all that you say but is all ok!

I think too that the problem are the bitmap but i don' know how can resolve it :(

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

pkrcel
Member #14,001
February 2012

How did you check? Where does the program throw the exception?

It is unlikely that Google shares your distaste for capitalism. - Derezo
If one had the eternity of time, one would do things later. - Johan Halmén

Jawall
Member #15,162
June 2013

Thanks you! i have resolved this problem! But now there is another problem :(

I have added this 2 new function at my program and there is again a Segmantation Fault:

#SelectExpand
1void InitStar() { 2 for(int j=0;j<MAT_SIZE;j++) 3 for(int i=0;i<MAT_SIZE;i++) { 4 int x = rand()%NUM_COLOR; 5 star[i+(MAT_SIZE*j)].color = x; 6 star[i+(MAT_SIZE*j)].posx = i; 7 star[i+(MAT_SIZE*j)].posy = j; 8 star[i+(MAT_SIZE*j)].live = true; 9 } 10} 11 12void DrawCandies(ALLEGRO_BITMAP *candy,int startx, int starty) { 13 int frameWidth = al_get_bitmap_width(candy) / NUM_COLOR; 14 for(int j=0;j<MAT_SIZE;j++) 15 for(int i=0;i<MAT_SIZE;i++) { 16 al_draw_bitmap_region(candy,star[i+(MAT_SIZE*j)].color*frameWidth,0,frameWidth,frameWidth,star[i+(MAT_SIZE*j)].posx*frameWidth+startx,star[i+(MAT_SIZE*j)].posy*frameWidth+starty,0); 17 } 18}

I don't believe it, all to me! :'( Where is now the error?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Where do you declare your 'star' array? How big is it? Does it have room for MATSIZE*MATSIZE elements?

Also, this is unreadable :

Quote:

 for(int i=0;i<MAT_SIZE;i++) { al_draw_bitmap_region(candy,star[i+(MAT_SIZE*j)].color*frameWidth,0,frameWidth,frameWidth,star[i+(MAT_SIZE*j)].posx*frameWidth+startx,star[i+(MAT_SIZE*j)].posy*frameWidth+starty,0); }

extract the object from star[i + j*MAT_SIZE] first.

   STAR s = star[i + j*MAT_SIZE];
   al_draw_bitmap_region(candy , 
                         s.color*frameWidth , 0 , frameWidth , frameWidth ,
                         s.posx*frameWidth + startx , s.posy*frameWidth + starty, 0);

Of course you may run into issues with the copy constructor then, but not if it is just a static struct with public members. Otherwise you could just use a reference instead.

Jawall
Member #15,162
June 2013

star array is a global variable so should see it...

struct Star {
    int color;
    int posx;
    int posy;
    bool live;

};

this is the struct..

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Ok I see. Missed that. Well then 'candy' is probably null again. You should run it through a debugger and go to the call stack and get a backtrace of the frames the program is in. That way you know the exact line the program is crashing on.

Jawall
Member #15,162
June 2013

Now i tried to comment where i call those function in the main but the error of segmentation fault remains.... So their aren't the problem...

Before of put in the main the functions the program worked (drawing the grid), after put in the 2 function the program no worked, i try to remove (comment) them and no work ... there is something of strange ???

EDIT: I have resolved!!! The problem was again a image that now i have changed but before it ran anyway :/ However thanks for the help :)

Go to: