gcc 4.5.1 c89
我正在尝试释放一些内存。但是,当我检查 valgrind 时,内存并没有被释放。我想知道我做错了什么。
我有以下结构:
typedef struct tag_cand_results {
char *candidate_winners[NUMBER_OF_CANDIDATES];
} cand_results;
我创建了这个结构的对象:
cand_results *results = NULL;
我为结构分配了一些内存。
results = calloc(1, sizeof *results);
为其分配一些数据
results->candidate_winners[0] = strdup("Steve Martin");
results->candidate_winners[1] = strdup("Jack Jones");
然后我尝试释放所有分配的内存:
free(results->candidate_winners[0]);
free(results->candidate_winners[1]);
free(results);
Just to be safe assign to NULL
results = NULL;
我从 valgrind 得到以下输出。
==8119== 72 bytes in 6 blocks are definitely lost in loss record 1 of 2
==8119== at 0x4A05E46: malloc (vg_replace_malloc.c:195)
==8119== by 0x3FE2E82A91: strdup (strdup.c:43)
==8119== by 0x400E5A: main (driver.c:116)
==8119==
==8119== 72 bytes in 6 blocks are definitely lost in loss record 2 of 2
==8119== at 0x4A05E46: malloc (vg_replace_malloc.c:195)
==8119== by 0x3FE2E82A91: strdup (strdup.c:43)
==8119== by 0x400E72: main (driver.c:117)
不知道为什么内存没有被释放?
非常感谢您的任何建议,