【问题标题】:Valgrind reporting too many mallocsValgrind 报告过多的 malloc
【发布时间】:2015-06-12 06:20:55
【问题描述】:

考虑这段代码:

int main(int argc, char const *argv[])
{
    char *string = NULL;
    string = malloc(sizeof(char) * 30);
    free(string);
    return 0;
}

我 malloc 一个 char 指针,然后我释放它。现在考虑 valgrind 的输出:

==58317== Memcheck, a memory error detector
==58317== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==58317== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==58317== Command: ./a.out
==58317== 
==58317== 
==58317== HEAP SUMMARY:
==58317==     in use at exit: 34,941 bytes in 424 blocks
==58317==   total heap usage: 505 allocs, 81 frees, 41,099 bytes allocated
==58317== 
==58317== LEAK SUMMARY:
==58317==    definitely lost: 0 bytes in 0 blocks
==58317==    indirectly lost: 0 bytes in 0 blocks
==58317==      possibly lost: 0 bytes in 0 blocks
==58317==    still reachable: 0 bytes in 0 blocks
==58317==         suppressed: 34,941 bytes in 424 blocks
==58317== 
==58317== For counts of detected and suppressed errors, rerun with: -v
==58317== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

怎么可能有这么多的 malloc 和 free?

编辑:这是我使用valgrind --leak-check=yes --gen-suppressions=all ./a.out 运行时得到的,我正在尝试制作一个 supp 文件。

==60943== Memcheck, a memory error detector
==60943== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==60943== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==60943== Command: ./a.out
==60943== 
==60943== 
==60943== HEAP SUMMARY:
==60943==     in use at exit: 34,941 bytes in 424 blocks
==60943==   total heap usage: 505 allocs, 81 frees, 41,099 bytes allocated
==60943== 
==60943== LEAK SUMMARY:
==60943==    definitely lost: 0 bytes in 0 blocks
==60943==    indirectly lost: 0 bytes in 0 blocks
==60943==      possibly lost: 0 bytes in 0 blocks
==60943==    still reachable: 0 bytes in 0 blocks
==60943==         suppressed: 34,941 bytes in 424 blocks
==60943== 
==60943== For counts of detected and suppressed errors, rerun with: -v
==60943== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 15)

【问题讨论】:

  • 我无法在这里重现 - 你确定你的 a.out 实际上是从上面的代码构建的吗?
  • 用你的代码我得到了==14910== total heap usage: 1 allocs, 1 frees, 30 bytes allocated你是什么标题?你如何编译二进制文件?
  • gcc -g main.c 我确信我的 a.out 是由上面的代码构建的。
  • 哪个操作系统?
  • 我也收到了total heap usage: 1 allocs, 1 frees, 30 bytes allocated - 也许您的 gcc 和/或 valgrind 版本有些奇怪?

标签: c malloc valgrind free


【解决方案1】:

这些块由链接到您的可执行文件的系统库分配(其中一些也被释放)。

Valgrind 有一个默认的 suppression 文件,可以抑制系统库中的泄漏,您可以在输出中进一步看到:

==58317==         suppressed: 34,941 bytes in 424 blocks

如果您想详细了解究竟是什么被抑制了,您可以使用-v 选项。

【讨论】:

  • @user3711622 :是的 - 它们通常是误报,而且只有有时系统库中存在难以修复的真正泄漏。但我从来没有见过它们的数量太多。换句话说:如果默认情况下它们被抑制,它们应该可以安全地忽略。
  • 如何从我的输出中“隐藏”这些内容,以便查看我造成的内存泄漏?
  • @user3711622 :这就是抑制文件的用途 - 它抑制了这些已知的泄漏,因此您可以专注于真实的泄漏(例如,通过查看泄漏摘要中的其他行)。跨度>
  • @user3711622 :尝试从您的代码中删除 free(string); 行 - 然后 valgrind 输出应该报告泄漏(与被抑制的泄漏明显区分开来)。
  • @user3711622 :您通常不能使用 valgrind 的泄漏检查器。但这不是它的用途。你想要的是一个内存分析器,比如massif。 (或任何其他你喜欢的东西)。由于这不再与您的原始问题相关,因此有关该问题的任何其他问题都属于新问题。
猜你喜欢
  • 1970-01-01
  • 2012-01-27
  • 2011-07-01
  • 1970-01-01
  • 2012-06-12
  • 1970-01-01
  • 2014-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多