|
From: Rob M. <rl...@pr...> - 2004-12-07 17:12:32
|
I have a problem with a memory leak in another program that uses the
tsearch family from glibc (v2.3.2, under RHEL 3 update 3). I wrote a
short test program to test it, but what I get from valgrind is a random
number of bytes of memory that aren't freed! Following, the program:
================
#include <search.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void *root=NULL;
void *xmalloc(unsigned n) {
void *p;
p = malloc(n);
if(p) return p;
fprintf(stderr, "insufficient memory\n");
exit(1);
}
int compare(const void *pa, const void *pb) {
if(*(int *)pa < *(int *)pb) return -1;
if(*(int *)pa > *(int *)pb) return 1;
return 0;
}
void action(const void *nodep, const VISIT which, const int depth)
{
int *datap;
if (which == postorder || which == leaf)
{
int i;
for (i=0; i<depth; i++)
fputs(" ",stdout);
datap = *(int **)nodep;
printf("%6d\n", *datap);
}
}
static void free_deq_node(void *nodep)
{
// printf (" freeing: %08lx -> %d\n",nodep,*(int*)nodep);
free(nodep);
}
int main()
{
int i, j, *ptr;
void *val;
srand(time(NULL));
for (j=0; j<10; j++)
{
root = NULL;
// printf("*** %d ***\n",j);
fflush(stdout);
for (i = 0; i < 12; i++) {
ptr = (int *)xmalloc(sizeof(int));
*ptr = rand()&0xff;
// printf (" in: %08lx -> %d\n",ptr,*ptr);
val = tsearch((void *)ptr, &root, compare);
if(val == NULL) exit(1);
}
// printf (" out, %08lx -> %d\n",ptr,*ptr);
tdelete(ptr,&root,&compare);
free(ptr);
tdestroy(root,&free_deq_node);
}
}
================
And the results:
================
==10013== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from 1)
==10013== malloc/free: in use at exit: 12 bytes in 3 blocks.
==10013== malloc/free: 237 allocs, 234 frees, 2352 bytes allocated.
==10013== For counts of detected errors, rerun with: -v
==10013== searching for pointers to 3 not-freed blocks.
==10013== checked 1422280 bytes.
==10013==
==10013== 12 bytes in 3 blocks are definitely lost in loss record 1 of 1
==10013== at 0x1B903D38: malloc (vg_replace_malloc.c:131)
==10013== by 0x804865C: xmalloc (foo.c:10)
==10013== by 0x80487B5: main (foo.c:55)
==10013==
==10013== LEAK SUMMARY:
==10013== definitely lost: 12 bytes in 3 blocks.
==10013== possibly lost: 0 bytes in 0 blocks.
==10013== still reachable: 0 bytes in 0 blocks.
==10013== suppressed: 0 bytes in 0 blocks.
==10016== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from 1)
==10016== malloc/free: in use at exit: 16 bytes in 4 blocks.
==10016== malloc/free: 236 allocs, 232 frees, 2336 bytes allocated.
==10016== For counts of detected errors, rerun with: -v
==10016== searching for pointers to 4 not-freed blocks.
==10016== checked 1422284 bytes.
==10016==
==10016== 16 bytes in 4 blocks are definitely lost in loss record 1 of 1
==10016== at 0x1B903D38: malloc (vg_replace_malloc.c:131)
==10016== by 0x804865C: xmalloc (foo.c:10)
==10016== by 0x80487B5: main (foo.c:55)
==10016==
==10016== LEAK SUMMARY:
==10016== definitely lost: 16 bytes in 4 blocks.
==10016== possibly lost: 0 bytes in 0 blocks.
==10016== still reachable: 0 bytes in 0 blocks.
==10016== suppressed: 0 bytes in 0 blocks.
==10019== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from 1)
==10019== malloc/free: in use at exit: 8 bytes in 2 blocks.
==10019== malloc/free: 238 allocs, 236 frees, 2368 bytes allocated.
==10019== For counts of detected errors, rerun with: -v
==10019== searching for pointers to 2 not-freed blocks.
==10019== checked 1422276 bytes.
==10019==
==10019== 8 bytes in 2 blocks are definitely lost in loss record 1 of 1
==10019== at 0x1B903D38: malloc (vg_replace_malloc.c:131)
==10019== by 0x804865C: xmalloc (foo.c:10)
==10019== by 0x80487B5: main (foo.c:55)
==10019==
==10019== LEAK SUMMARY:
==10019== definitely lost: 8 bytes in 2 blocks.
==10019== possibly lost: 0 bytes in 0 blocks.
==10019== still reachable: 0 bytes in 0 blocks.
==10019== suppressed: 0 bytes in 0 blocks.
The invocation was
valgrind --tool=memcheck --leak-check=yes --show-reachable=yes
--leak-resolution=high a.out
This is using valgrind 2.2.0.
Any help you could offer would be gratefully received.
|