NOGDUS

Articles, Tutorials, and other things. => General Game Programming => Topic started by: Richard Marks on November 06, 2008, 06:19:46 PM



Title: Checking Memory Leaks in Allegro and SDL with Valgrind
Post by: Richard Marks on November 06, 2008, 06:19:46 PM
I prepared these two very simple test programs that simply open a window and then close to compare the memory leaks in both Allegro and SDL.

testAllegro.cpp
Code:
#include <allegro.h>

int main(int argc, char* argv[])
{
allegro_init();
install_keyboard();
set_color_depth(24);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
allegro_exit();
return 0;
}

testSDL.cpp
Code:
#include <SDL.h>

int main(int argc, char* argv[])
{
atexit(SDL_Quit);
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 24, SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_FreeSurface(screen);
return 0;
}

I then compiled & linked them, then ran them like this:

Code:
$ valgrind -v --leak-check=full --show-reachable=yes ./testAllegro > testAllegro.txt 2>&1
$ valgrind -v --leak-check=full --show-reachable=yes ./testSDL > testSDL.txt 2>&1

Here are the results of using Valgrind.

Results for testAllegro (http://www.ccpssolutions.com/storage/richard/memleaks/testAllegro.txt)

Results for testSDL (http://www.ccpssolutions.com/storage/richard/memleaks/testSDL.txt)


If you don't want to sift through all of that madness, here is what caught my eye.

from testAllegro:

Code:
==18964== IN SUMMARY: 13 errors from 5 contexts (suppressed: 58 from 1)
==18964==
==18964== malloc/free: in use at exit: 48,804 bytes in 710 blocks.
==18964== malloc/free: 6,065 allocs, 5,355 frees, 1,427,077 bytes allocated.
==18964== LEAK SUMMARY:
==18964==    definitely lost: 0 bytes in 0 blocks.
==18964==      possibly lost: 0 bytes in 0 blocks.
==18964==    still reachable: 48,804 bytes in 710 blocks.
==18964==         suppressed: 0 bytes in 0 blocks.
--18964--  memcheck: sanity checks: 48 cheap, 3 expensive

from testSDL:

Code:
==19020== IN SUMMARY: 323 errors from 16 contexts (suppressed: 176 from 1)
==19020==
==19020== malloc/free: in use at exit: 224,583 bytes in 4,283 blocks.
==19020== malloc/free: 15,371 allocs, 11,088 frees, 3,057,112 bytes allocated.
==19020== LEAK SUMMARY:
==19020==    definitely lost: 100 bytes in 5 blocks.
==19020==    indirectly lost: 592 bytes in 18 blocks.
==19020==      possibly lost: 58,672 bytes in 818 blocks.
==19020==    still reachable: 165,219 bytes in 3,442 blocks.
==19020==         suppressed: 0 bytes in 0 blocks.
--19020--  memcheck: sanity checks: 204 cheap, 9 expensive

Okay...I think that Allegro won the comparison test!


Title: Re: Checking Memory Leaks in Allegro and SDL with Valgrind
Post by: RedSlash on November 06, 2008, 07:58:57 PM
Most/all of the leaks in both examples come from X11/glibc. This is normal because X11 uses garbage collection and memory recycling and will keep memory around even after your programs have exited. If you eliminate all X11/GLibc related memory leaks through valgrind suppressions, these libraries should report 0. BUT, the SDL audio memory leak entries are suspicious though...