next up previous
Next: Debug levels Up: Implementation Previous: Large input file handling

Memory leak detector

Since every allocation call is logged in xmemory , it is easy to use the module as a memory leak detector. The way to do it is to call the xmemory_status() function before the program exits, at a point where no memory block should remain allocated. This function will remain silent if all memory has been deallocated, or print out a list of still allocated pointers, where they have been allocated, their size and type. Example:

#include <stdio.h>
#include "xmemory.h"

int main(void)
{
    int * ip ;

    ip = malloc(4 * sizeof(int));
    xmemory_status();
    return 0 ;
}

In xmemory version 2.0 and later, the output of this program looks like:

#----- memory status called from a.c (9) --------
#- ALL status
ALL_npointers       1
ALL_size            16
ALL_maxalloc_kb     0
#- RAM status
RAM_alloc           16
#- pointer details
(0x804bdc8) from a.c (8) in RAM  has 16 bytes

If you add a free(ip) statement before the end, xmemory_status() will not report anything.

The xmemory_status() function may print out diagnostics in addition to the memory status if the debug level is high enough.



Nicolas Devillard 2002-05-03