Segmentation fault error after calling a function
Battyhal

Hi everybody !.

I'm on my way to learn programming games using Allegro 5.I've written a function which simply act as a game Intro ,displaying a message on screen,resting for 1,05 seconds and continue with the game...unless the player (me ,for the moment) press ESCAPE to skip the intro .I have this code :

void Intro1()
{
bool esc = false;
while(!esc)
{
ALLEGRO_EVENT_QUEUE *eventIntro1 = NULL;
eventIntro1=al_create_event_queue();
al_register_event_source(eventIntro1,al_get_keyboard_event_source());

ALLEGRO_EVENT evIntro1;
al_wait_for_event(eventIntro1 ,&evIntro1);

ALLEGRO_SAMPLE *sampleIntro1 = NULL;
sampleIntro1 = al_load_sample("Intro1.ogg");

if(al_event_queue_is_empty(eventIntro1))
{
for(int i=0;i<=256;i++)
{
al_play_sample(sampleIntro1,1,0,1,ALLEGRO_PLAYMODE_ONCE, NULL);
ALLEGRO_FONT *font44 = al_load_font("215000EURO.ttf",44,0);

al_clear_to_color(al_map_rgb(0,0,0));

al_draw_text(font44,al_map_rgb(0,0,i),WIDTH/2,HEIGHT/3,
ALLEGRO_ALIGN_CENTER,"WELCOME TO ...");

al_flip_display();

al_destroy_font (font44);
al_destroy_sample(sampleIntro1);
}
}
else if(evIntro1.type == ALLEGRO_KEY_UP)
{
switch(evIntro1.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
esc = true;
break;
}
}
al_destroy_event_queue(eventIntro1);
}
al_rest(1.05);
esc = true;
}

It compiles without any problems ,but nothing appears on the screen ,and when i press ESCAPE,i've got a "Segmentation Fault (core dumped)" error and i do not know why .What i expect for this function to do is : there are no events in the queue ,so it draws "WELCOME TO..." on the screen ,rests for 1,05 seconds and terminates,unless a key is pressed (ESCAPE) and terminates immediately .Am i wrong?.Could somebody help me please ? .

I really apreciate it !:D

Edit:Sorry ,i forgot to tell i 'm using Ubuntu and Code::Blocks .And the function is part of a larger program which works fine when i comment or simply delete the function from it .

Chris Katko

Are you sure the Allegro is setup properly BEFORE you call the function, and all bitmaps are allocated?

Also, don't forget to use codeblocks so we can read it easier:

#SelectExpand
1void Intro1() 2{ 3bool esc = false; 4while(!esc) 5{ 6ALLEGRO_EVENT_QUEUE *eventIntro1 = NULL; 7eventIntro1=al_create_event_queue(); 8al_register_event_source(eventIntro1,al_get_keyboard_event_source()); 9 10ALLEGRO_EVENT evIntro1; 11al_wait_for_event(eventIntro1 ,&evIntro1); 12 13ALLEGRO_SAMPLE *sampleIntro1 = NULL; 14sampleIntro1 = al_load_sample("Intro1.ogg"); 15 16if(al_event_queue_is_empty(eventIntro1)) 17{ 18for(int i=0;i<=256;i++) 19{ 20al_play_sample(sampleIntro1,1,0,1,ALLEGRO_PLAYMODE_ONCE, NULL); 21ALLEGRO_FONT *font44 = al_load_font("215000EURO.ttf",44,0); 22 23al_clear_to_color(al_map_rgb(0,0,0)); 24 25al_draw_text(font44,al_map_rgb(0,0,i),WIDTH/2,HEIGHT/3, 26ALLEGRO_ALIGN_CENTER,"WELCOME TO ..."); 27 28al_flip_display(); 29 30al_destroy_font (font44); 31al_destroy_sample(sampleIntro1); 32} 33} 34else if(evIntro1.type == ALLEGRO_KEY_UP) 35{ 36switch(evIntro1.keyboard.keycode) 37{ 38case ALLEGRO_KEY_ESCAPE: 39esc = true; 40break; 41} 42} 43al_destroy_event_queue(eventIntro1); 44} 45al_rest(1.05); 46esc = true; 47}

I'm no A5 guy... but... why are you making new event queues over and over in the while loop?

Battyhal

Uff ,sorry about codeblocks !.

And for the Allegro setup and fonts and sounds ,I'm pretty sure they are ok ,was the first thing i checked when i received the error.

Are you saying i should have place the event queue outside and right before the while loop ? I did it in that way because i thought the function needs to read if an event has ocurred (in this case press ESCAPE key) to interrupt the loop .And i thought that was the way to do it...

But i suppose i was wrong.

Chris Katko

You create an event queue, and then set it to process events. A queue holds events. You shouldn't need to create an event queue for every event, or for every frame drawn.

Your code should look more like this example:

https://wiki.allegro.cc/index.php?title=Allegro_5_Tutorial/Events#Event_Queues

#SelectExpand
1#include <stdio.h> 2#include <allegro5/allegro.h> 3 4int main(int argc, char **argv){ 5 6 ALLEGRO_DISPLAY *display = NULL; 7 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 8 9 if(!al_init()) { 10 fprintf(stderr, "failed to initialize allegro!\n"); 11 return -1; 12 } 13 14 display = al_create_display(640, 480); 15 if(!display) { 16 fprintf(stderr, "failed to create display!\n"); 17 return -1; 18 } 19 20 event_queue = al_create_event_queue(); 21 if(!event_queue) { 22 fprintf(stderr, "failed to create event_queue!\n"); 23 al_destroy_display(display); 24 return -1; 25 } 26 27 al_register_event_source(event_queue, al_get_display_event_source(display)); 28 29 al_clear_to_color(al_map_rgb(0,0,0)); 30 31 al_flip_display(); 32 33 while(1) 34 { 35 ALLEGRO_EVENT ev; 36 ALLEGRO_TIMEOUT timeout; 37 al_init_timeout(&timeout, 0.06); 38 39 bool get_event = al_wait_for_event_until(event_queue, &ev, &timeout); 40 41 if(get_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 42 break; 43 } 44 45 al_clear_to_color(al_map_rgb(0,0,0)); 46 al_flip_display(); 47 } 48 49 al_destroy_display(display); 50 al_destroy_event_queue(event_queue); 51 52 return 0; 53}

Notice how there is only one event queue, it is created (once), used, and then removed (once).

Battyhal

Yes ,i had one event queue created in the main function but when i tried to register events from it inside the function i wrote (Intro) i received "event_queue X was not declared in this scope" ,that's why i created another inside the function .With the intention of destroy it after use it .

Chris Katko

Well, you should always use "dependency injection." That means anything you use in main, is forwarded to the function you use as a function argument, so it forces you to think about how much your function really touches.

Secondly, even if you used a second event queue, you are creating it INSIDE the while loop, which means you're creating a new one EVERY TIME that while loop cycles.

Battyhal

I've never heard about "Dependency Injection ",so i think i need to read something (or a lot) of it .

Thanks very much for the teachings Chris !!.

beoran

Or, in a global variable could also work, although some people would recommend against the use of global variables because they can get quite messy if abused.

Battyhal

Thanks for the advice beoran!.I need to read about that too!.

Meanwhile i will try to rewrite the function or integrate it in a game state.

Thread #615141. Printed from Allegro.cc