[AD] Memory debuggers

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Hi,

I recently downloaded Fortify,
http://www.geocities.com/SiliconValley/Horizon/8596/fortify.html, which
proved to be an extremely useful memory debugger (I don't think I will
ever develop a program again without it). It finds bugs like memory
leaks, writes to unallocated memory, freeing memory that was never
allocated etc. After testing it on some of my own programs, and after
tighting some leaks and other bad things they were doing, I decided to
test it on Allegro. Guess what? It immediately found two memory leaks,
one in demo.c and one in istretch.c (I'll send a patch soon to fix
this). I have only had time to test it with the demo; I'll test the
other programs when I get time. If someone wants to help I could send
him a patch that installs Fortify on Allegro 3.9.28. btw, with Fortify
it ought to be much easier to find the bug in quantize.

OK, to my point: Fortify works by doing '#define malloc Fortify_malloc'
and simliar things for realloc, free etc. This implies that even if you
use it in your program, it won't give you any help with pointers that
Allegro allocated unless you patch Allegro (eg if you create_bitmap()
and forget to destroy it, Fortify won't find the leak). Allegro could
quite easily be fixed to allow this though, if we made all Allegro code
use function pointers when calling malloc, calloc, realloc and free.
That way any program could redirect those pointers to Fortify's
functions instead of libc's (libc's functions would be the default of
course). Like this: 

in allegro.h or perhaps aintern.h:
   AL_FUNCPTR(void *, al_malloc, (int size));

in allegro.c:
   void *(*al_malloc)(int size) = malloc;

in Allegro's source files, replace
   my_pointer = malloc(4711);
with
   my_pointer = al_malloc(4711);

in my program, before allegro_init():
   al_malloc = my_wrapper_to_Fortify_malloc;

Other memory debuggers work in a similar way so this is not restricted
to Fortify. This would also make it easier for people to do clever
things when malloc fails, eg to free(_scratch_mem) or
unload_datafile(other_levels_in_the_game_than_the_one_currently_used)
and then try again.

Is this cool enough that Allegro should support it? I'd volunteer to
write the code, ie do the search-and-replace.

Sven



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/