【问题标题】:C complex memory leakC 复杂的内存泄漏
【发布时间】:2013-07-01 20:05:43
【问题描述】:

我的程序中有轻微的内存泄漏,我不确定它是在我的分配中还是在内部 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 字节的倍数,因此很可能是整数泄漏。

提前致谢

【问题讨论】:

  • 泄漏是否取决于列表的大小?

标签: c memory malloc memory-leaks


【解决方案1】:

我刚刚在 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 字节泄漏,所以我猜这是误报。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-11
    • 2016-01-27
    • 2010-11-11
    • 2017-02-18
    • 1970-01-01
    • 2013-06-15
    • 2015-11-19
    • 2014-11-01
    相关资源
    最近更新 更多