I know there are programs out there that replace the malloc in the standard
libraries with one that does automatic tracing. However, I never use them..
instead I have my own functions to trace them which works perfectly well for
me. Essentially I keep track of all the different datatypes I dynamically
allocate -- since I have a pretty good feeling of when different types are allocated I
can just look at current counts to find leaks:
It goes something like this:
--------------------------------------------------------------------------------
enum {
MEM_TYPE_APPLE,
MEM_TYPE_ORANGE,
MEM_MAX_TYPES
} types;
int alloc_count[MEM_MAX_TYPES];
initialize_alloc(){
int i;
for(i=0;i<MEM_MAX_TYPES;i++){
alloc_count[i] = 0;
}
}
void *mymalloc(int size, int index)
{
void *tmp;
tmp = malloc(size);
alloc_count[index]++;
}
void myfree(void *ptr, int index)
{
free(ptr);
alloc_count[index]--;
}
void dump_counters(void)
{
for(i=0;i<MEM_MAX_TYPES;i++){
if(alloc_count[i] > 0)
printf("%d: %d allocated\n",i,alloc_count[i]);
}
}
--------------------------------------------------------------------------------
Then I just replace all calls to malloc and free with alloc, and the
type from memtypes which is approprate.. for instance:
p = (apple *)mymalloc(sizeof(apple),MEM_TYPE_APPLE));
You can also look at the counters in core dumps since alloc_count is a
global -- this will work for you if you are in an environment where you
can't or don't want to use printf to dump the counters. In linux, you
can just find out the process idnum and copy the core from /proc/idnum/*
Cheers,
Chris
> Hello
>
> Is there a decent way to trace memory leaks?
>
> TIA,
>
> Mihai
>
> Mihai Ibanescu Dynamic Network Technologies
> http://sysadm.dntis.ro/~misa Moara de Foc 35, et. 7, 6600 Iasi
> [EMAIL PROTECTED] tel. +40-32-252936
>