Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » al_uninstall_system excutes a function called "shutdown" if present

This thread is locked; no one can reply to it. rss feed Print
al_uninstall_system excutes a function called "shutdown" if present
Roy Balela
Member #16,645
March 2017

I was experiencing some random crashes when uninstalling Allegro.

I found out that if I have a function called shutdown(), al_uninstall_system will execute it twice when called.

I'm compiling with GCC on Linux. Compiling with G++ does not reproduce the error.

  • Allegro v5.2.4.1

  • GCC v4.8.4

  • Linux v3.13.0-37

If I rename the function, no error or warnings are emitted.

The code below runs fine, but shutdown() is called twice when Allegro is uninstalled.

#SelectExpand
1#include <stdio.h> 2#include <stdlib.h> 3#include <allegro5/allegro.h> 4#include <allegro5/allegro_font.h> 5 6ALLEGRO_DISPLAY *display; 7ALLEGRO_FONT *font; 8 9void abort_on_error(const char *message) { 10 fprintf(stderr, "%s\n", message); 11 exit(EXIT_FAILURE); 12} 13 14void setup() { 15 if (!al_install_system(ALLEGRO_VERSION_INT, NULL)) 16 abort_on_error("Failed to init Allegro"); 17 18 display = al_create_display(320, 240); 19 if (!display) abort_on_error("Failed to create window"); 20 21 al_set_window_title(display, "Allegro Test"); 22 23 font = al_create_builtin_font(); 24 if (!font) abort_on_error("Failed to create font"); 25} 26 27void shutdown() { 28 printf("Calling shutdown()\n"); 29} 30 31void real_shutdown() { 32 al_destroy_font(font); 33 al_destroy_display(display); 34 printf("al_uninstall_system() start\n"); 35 al_uninstall_system(); 36 printf("al_uninstall_system() end\n"); 37} 38 39void play() { 40 al_clear_to_color(al_map_rgb_f(0,0,0)); 41 al_draw_text(font, al_map_rgb_f(1,1,1), 10, 10, 0, "Allegro Test"); 42 al_flip_display(); 43 al_rest(2); 44} 45 46int main (int argc, char *argv[]) { 47 setup(); 48 play(); 49 real_shutdown(); 50 return 0; 51}

Output:

al_uninstall_system() start
Calling shutdown()
Calling shutdown()
al_uninstall_system() end

DanielH
Member #934
January 2001
avatar

Am I blind, but when do you call "void shutdown()"?

I see you're program prints "Calling shutdown()", but it was never called.

torhu
Member #2,727
September 2002
avatar

Just put a breakpoint on shutdown() to see where it is being called from?

As you probably know you would normally just call al_init, not al_install_system or al_uninstall_system.

Edgar Reynaldo
Member #8,592
May 2007
avatar

It's a linking bug. Allegro sees your shutdown function and calls it mistakenly, thinking it is its own version of shutdown.

You can debug this easily by setting a breakpoint in your shutdown function. Then when allegro calls it, gdb will stop and let you get a backtrace. Do it twice. Post the results.

Roy Balela
Member #16,645
March 2017

Backtrace:

#0  shutdown () at test.c:28
#1  0x00007ffff5de0786 in xcb_disconnect ()
   from /usr/lib/x86_64-linux-gnu/libxcb.so.1
#2  0x00007ffff6caf4e7 in XCloseDisplay ()
   from /usr/lib/x86_64-linux-gnu/libX11.so.6
#3  0x00007ffff7b77a17 in ?? ()
   from /usr/lib/x86_64-linux-gnu/liballegro.so.5.2
#4  0x00007ffff7b2bcd5 in ?? ()
   from /usr/lib/x86_64-linux-gnu/liballegro.so.5.2
#5  0x00007ffff7b1fd4d in _al_run_exit_funcs ()
   from /usr/lib/x86_64-linux-gnu/liballegro.so.5.2
#6  0x00007ffff7b2bdb5 in al_uninstall_system ()
   from /usr/lib/x86_64-linux-gnu/liballegro.so.5.2
#7  0x0000000000400c89 in real_shutdown () at test.c:35
#8  0x0000000000400da5 in main (argc=1, argv=0x7fffffffe158) at test.c:49

It's a linking bug. Allegro sees your shutdown function and calls it mistakenly, thinking it is its own version of shutdown.

Apparently, yes. I just found out shutdown() is POSIX function(https://linux.die.net/man/2/shutdown), so I better not have another function with the same name.

Thank you! ;D

Go to: