Possible memory leak in allegro 5.0.10

OK, here is the code I (mostly) took from wiki:

1#define CRTDBG_MAP_ALLOC 2#include <stdlib.h> 3#include <crtdbg.h> 4 5#include "allegro5/allegro.h" 6#include "allegro5/allegro_image.h" 7#include "allegro5/allegro_native_dialog.h" 8 9int main(int argc, char **argv) 10{ 11 _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF |_CRTDBG_LEAK_CHECK_DF ); 12 ALLEGRO_DISPLAY *display = NULL; 13 ALLEGRO_BITMAP *image = NULL; 14 15 if(!al_init()) 16 { 17 al_show_native_message_box(display, "Error", "Error", "Failed to initialize allegro!", 18 NULL, ALLEGRO_MESSAGEBOX_ERROR); 19 return 0; 20 } 21 22 if(!al_init_image_addon()) 23 { 24 al_show_native_message_box(display, "Error", "Error", "Failed to initialize al_init_image_addon!", 25 NULL, ALLEGRO_MESSAGEBOX_ERROR); 26 return 0; 27 } 28 29 display = al_create_display(800,600); 30 31 if(!display) 32 { 33 al_show_native_message_box(display, "Error", "Error", "Failed to initialize display!", 34 NULL, ALLEGRO_MESSAGEBOX_ERROR); 35 return 0; 36 } 37 38 image = al_load_bitmap("image.png"); 39 40 if(!image) 41 { 42 al_show_native_message_box(display, "Error", "Error", "Failed to load image!", 43 NULL, ALLEGRO_MESSAGEBOX_ERROR); 44 al_destroy_display(display); 45 return 0; 46 } 47 48 al_draw_bitmap(image,200,200,0); 49 50 al_flip_display(); 51 al_rest(2); 52 53 al_destroy_display(display); 54 al_destroy_bitmap(image); 55 al_shutdown_image_addon(); 56 al_uninstall_system(); 57 58 return 0; 59}

I also added the _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF |_CRTDBG_LEAK_CHECK_DF ) line which is a crt function which spots memory leaks and dumps them to output at program termination (http://msdn.microsoft.com/en-us/library/vstudio/x98tx3cf.aspx)
When the program terminates, I get the following output:

Detected memory leaks!

Dumping objects -> {564} normal block at 0x007CB008, 65536 bytes long. Data: <+Da +Da +Da +Da > 2B 44 61 FF 2B 44 61 FF 2B 44 61 FF 2B 44 61 FF Object dump complete.

I then set a breakpoint at block 564 allocation, and this block is allocated when allegro bitmap is loaded, specifically, in _al_d3d_create_bitmap(), you can see attachment to get the idea where it is.

Memory for d3d bitmap is probably not deallocated at program termination, despite explicit call to allegro_destroy_bitmap().

Or it may be false positive.

Arthur Kalliokoski

65536 bytes! Zomg!


Actually, there must be an error in this tutorial: http://wiki.allegro.cc/index.php?title=Basic_tutorial_on_loading_and_showing_images
When I free the image BEFORE freeing the display (which uses this image, probably), no memory leaks are detected.
I must say, though, that it is not specified anywhere, although seems natural. However, this tutorial really confused me.

P.S.: Can I edit the order of these calls in wiki?


Of course you can. Any contribution to the wiki is welcome and appreciated.

Matthew Leverton

When you destroy the display first, the bitmap is converted to a memory bitmap. So perhaps that process is leaking some memory.


It shouldn't though. If no one else takes this, I'll look into it after my holiday.

Thomas Fjellstrom

Holidays are the perfect time to work on side projects.


Unfortunately, not so if they are family holidays. Ah well, that's what it's like to be a father. :)

Thomas Fjellstrom

Nah, nothing says you actually have to spend all of your time on a holiday catering to your family ;)

Thread #613126. Printed from Allegro.cc