我的程序中有轻微的内存泄漏,我不确定它是在我的分配中还是在内部 c 结构中。我使用的唯一 malloc 是这些:
results = (int*) malloc (instance_n * sizeof (int) );
instances = (char**) malloc (instance_n * sizeof (char*) );
for (i = 0; i < instance_n; i++) {
instances[i] = (char*) malloc (1001 * sizeof (char) );
}
List_add (); (standard doubly linked list. Never gave me a problem)
我在同一个地方释放所有东西:
free (results);
List_clear (&dynamic);
for (i = 0; i < instance_n; i++) {
free (instances[i]);
}
free (instances);
顺便说一句:List_clear =
Node* node = list->last;
if (node == NULL) return;
while (node->previous != NULL)
{
node = node->previous;
free (node->next);
}
free (list->first);
此外,我正在使用 timeval 和 FILE 结构(文件在方法结束时关闭)
我错过了什么吗?对我来说,这看起来就像我正在释放一切。我以前从未遇到过内存泄漏问题,所以我在调试它时很糟糕,但 Valgrind 一直指出这个内存泄漏:
==3180== HEAP SUMMARY:
==3180== in use at exit: 62,951 bytes in 361 blocks
==3180== total heap usage: 556 allocs, 195 frees, 115,749 bytes allocated
==3180==
==3180== LEAK SUMMARY:
==3180== definitely lost: 8,624 bytes in 14 blocks
==3180== indirectly lost: 1,168 bytes in 5 blocks
==3180== possibly lost: 4,925 bytes in 68 blocks
==3180== still reachable: 48,234 bytes in 274 blocks
==3180== suppressed: 0 bytes in 0 blocks
==3180== Rerun with --leak-check=full to see details of leaked memory
==3180==
我不禁注意到“14 块”部分,但我的代码中没有任何部分分配少于 20 个部分,并且 8624 字节是 4 字节的倍数,因此很可能是整数泄漏。
提前致谢