Program crashes when creating and destroying a display multiple times
Mashed

Hi,

I am currently doing a complete redesign of the UI for a small to medium sized game project in C++, and at the same time migrating from A4 to A5.

Now in the tests I've written for the new code(I'm doing pseudo-test-driven-development :P), more than one test creates and destroys an ALLEGRO_DISPLAY (via a self-made window class).
At the second time, al_create_display crashes with a Segmentation Fault.
When running each test seperately (always creating exactly one display throughout execution), they exit normally.

Doing al_destroy_display(al_create_display(640,480)); several times in a row does not cause any problems, so I'm suspecting some other strange dependency here.

GDB backtrace:

driver = system->vt->get_display_driver();
(gdb) bt
#0  al_create_display (w=640, h=480) at [...]/allegro-5.0.6/src/display.c:46
#1  0x000000000040a952 in GameWindow::GameWindow (this=0x639ba0, width=640, 
height=480, event_dispatcher=0x6a06f0) at GameWindow.cpp:70
#2  0x0000000000424645 in GameWindowTest::runAllTests (this=0x73c0b0, 
activate_manual_check=false, activate_manual_input=false) at GameWindowTest.cpp:86
#3  0x0000000000420606 in main (argc=1, argv=[...]) at testenv.cpp:88

Valgrind reports an invalid read of size 8 on that line. (perhaps a pointer) No other errors are detected by Valgrind, and no errors when running tests individually.

I am using Allegro 5.0.6 on an Ubuntu 10.04 64bit system, Allegro was built without any special debug build options. Everything was compiled with gcc/g++ version 4.4.3.

The code I use to initialize Allegro:

bool Testcase::allegroInit()
{
  if(!al_init()) return false;
  if(!al_install_keyboard()) return false;
  if(!al_install_mouse()) return false;
  if(!al_init_image_addon()) return false;
  al_init_font_addon();
  if(!al_init_primitives_addon()) return false;
  return true;
}

And the code that calls al_create_display:

GameWindow::GameWindow(uint16_t width, uint16_t height,
                       EventDispatcher* event_dispatcher)
 : Entity(width, height, event_dispatcher, 0), resized_(false)
{
  int flags = ALLEGRO_WINDOWED | ALLEGRO_RESIZABLE;
  al_set_new_display_flags(flags);
  display_ = al_create_display(WINDOW_DEFAULT_WIDTH, WINDOW_DEFAULT_HEIGHT);
  assert(display_);
}

Do you guys have any idea what is going on? Am I missing something?(such as initialization of any kind)
Help would be much appreciated. If you need more information, I'll do my best to provide the very.

CodeStepper

Maybe i misunderstood, but look at this :)

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_primitives.h> 3#include <allegro5/allegro_font.h> 4#include <allegro5/allegro_ttf.h> 5#include <allegro5/allegro_image.h> 6#include <iostream> 7 8 9bool init_al( void ) 10{ 11 if(!al_init()) return false; 12 if(!al_install_keyboard()) return false; 13 if(!al_install_mouse()) return false; 14 if(!al_init_image_addon()) return false; 15 al_init_font_addon(); 16 if(!al_init_primitives_addon()) return false; 17 return true; 18} 19 20ALLEGRO_DISPLAY* create_display( int width, int height, int flags = 0 ) 21{ 22 al_set_new_display_flags( flags ); 23 ALLEGRO_DISPLAY *display = al_create_display( width, height ); 24 return display; 25} 26 27void delete_display( ALLEGRO_DISPLAY *display ) 28{ 29 al_destroy_display( display ); 30} 31 32int main( void ) 33{ 34 init_al( ); 35 36 ALLEGRO_DISPLAY *display = NULL; 37 38 display = create_display( 800, 600 ); 39 delete_display( display ); 40 display = create_display( 800, 600 ); 41 delete_display( display ); 42 display = create_display( 800, 600 ); 43 delete_display( display ); 44 display = create_display( 800, 600 ); 45 delete_display( display ); 46 display = create_display( 800, 600 ); 47 delete_display( display ); 48 display = create_display( 800, 600 ); 49 delete_display( display ); 50 display = create_display( 800, 600 ); 51 delete_display( display ); 52 display = create_display( 800, 600 ); 53 delete_display( display ); 54 display = create_display( 800, 600 ); 55 delete_display( display ); 56 display = create_display( 800, 600 ); 57 delete_display( display ); 58 display = create_display( 800, 600 ); 59 delete_display( display ); 60 display = create_display( 800, 600 ); 61 delete_display( display ); 62 display = create_display( 800, 600 ); 63 delete_display( display ); 64 display = create_display( 800, 600 ); 65 delete_display( display ); 66 display = create_display( 800, 600 ); 67 delete_display( display ); 68 display = create_display( 800, 600 ); 69 delete_display( display ); 70 display = create_display( 800, 600 ); 71 delete_display( display ); 72 73 74 return 0; 75}

Mashed

Sorry, I don't see it. :-/ Did you do anything different than me?

CodeStepper

The thing that did not
This code works the same way as yours, but it works
In the code that you gave everything is ok, aside from the fact that I do not see the declaration display_

I always use this-> in class ( for me is better, I made level up from php to cpp :P )
Code, given by you is ( for me ) correct...

Give more code to test, then maybe i could help you, because i really don't see anything wrong in this code :-/

previous post alluded rather to create function and test this on it ( better do something 2 times than once wrong )

bamccaig

I'm only guessing, but it might make sense if you are crossing thread boundaries..? Are you using threads?

Mashed

Thanks for your replies.
CodeStepper: The variable display_ is declared as a non-static class member. Also, I compiled and ran your code with both gcc and g++, no problems whatsoever.
bamccaig: No, I am not using threads, neither is the finished program when the stuff passed testing.

Tomorrow, I'll try to make a minimal program to reproduce the error.

EDIT: Cannot post a reply now, but I think I fixed it. Deleting the objects somehow corrupted the heap. I've had my suspicions, but everything suggested it was related to Allegro.

Thread #610829. Printed from Allegro.cc