gpt4 book ai didi

C 复杂的内存泄漏

转载 作者:行者123 更新时间:2023-11-30 17:50:07 25 4
gpt4 key购买 nike

我的程序中有轻微的内存泄漏,我不确定它是在我的分配中还是在内部 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 个 block ”部分,但我的代码中没有任何部分分配的部分少于 20 个部分,并且 8624 字节是 4 字节的倍数,因此很可能是整数泄漏。

提前致谢

最佳答案

我刚刚在 valgrind 上发现了这个条款:

==3821== WARNING: Support on MacOS 10.8 is experimental and mostly broken.
==3821== WARNING: Expect incorrect results, assertions and crashes.
==3821== WARNING: In particular, Memcheck on 32-bit programs will fail to
==3821== WARNING: detect any errors associated with heap-allocated data.

许多其他人在 MacOSX 10.8 上也遇到了相同的 8624 字节泄漏,所以我猜这是一个误报。

关于C 复杂的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17459752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com