On Monday 19 June 2006 7:55 am, Verdi March wrote:
Hi,
Valgrind detected memleak on one of my test case, which is a standard, textbook insertion sort. However, I don't see any problem with the testcase. I'm wondering if this is a false alarm, and should submit a bug report.
I'm using SuSE 10.1, and valgrind-3.2.0-11.1 from opensuse build service.
The followings are the test case and the output of valgrind. I've marked the offending line (line 36) in the testcase.
/************************** Test Case ************************/ #include <stdio.h> #include <stdlib.h>
typedef struct _int_struct { int i; struct _int_struct *next; } IntStruct;
typedef struct _int_list { IntStruct *head; } IntList;
int insert(IntList *ilist, int i); int insertion_sort(IntList *ilist, IntStruct *is);
int main() { IntStruct *is; IntList ilist;
ilist.head = NULL;
insert(&ilist, 500); insert(&ilist, 100);
printf("ilist: "); for (is = ilist.head; is; is = is->next) printf("%d ", is->i); printf("\n");
return 0; }
int insert(IntList *ilist, int i) { IntStruct *is = (IntStruct *) calloc(1, sizeof(IntStruct)); /* line 36 */ is->i = i; return insertion_sort(ilist, is); }
int insertion_sort(IntList *ilist, IntStruct *is) { IntStruct **isp; for (isp = &ilist->head; *isp; isp = &((*isp)->next)) { if (is->i < (*isp)->i) { is->next = *isp; break; } else if (is->i == (*isp)->i) { /* return FALSE; */ return 0; } }
*isp = is;
/* return TRUE; */ return 1; } You very definitely have a memory leak. You create your list using calloc(3), but you fail to free that list.
-- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9