【问题标题】:What all C operations does valgrind treat as 'malloc' and 'free'?valgrind 将哪些 C 操作视为“malloc”和“free”?
【发布时间】:2013-10-18 18:30:50
【问题描述】:

在工作中,我正在用 C 语言编写一个相当复杂的软件,并且我经常使用 valgrind 对其进行测试。到目前为止,该程序运行良好,没有内存泄漏或数组边界违规,并且在 valgrind 报告中,“frees”的数量与“mallocs”的数量相匹配 - 很棒。让我烦恼的是它报告了数千个 free 和 malloc。而且我知道一个事实我做的不超过50-60。我确实知道,当我的程序调用“fopen”时,valgrind 会将调用计入 malloc 的数量,同样,“fclose”也会计入“frees”的数量。但就我而言,这仍然不能解释我看到的内存被分配和释放多少次的数字。我仔细搜索了我的代码以寻找罪魁祸首,但我一无所获。由于明显的原因,我不能在这里发布任何代码,但我只想知道,我错过了什么吗?是否还有其他 C 操作使 valgrind 计入 malloc 和 frees 的数量?

这是我的 valgrind 报告。如您所见,从这个角度来看,一切看起来都很好。

Memcheck, a memory error detector
Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
Command: ./Codec
Parent PID: 3526

HEAP SUMMARY:
     in use at exit: 0 bytes in 0 blocks
   total heap usage: 2,407 allocs, 2,407 frees, 28,877,748 bytes allocated

 All heap blocks were freed -- no leaks are possible

 For counts of detected and suppressed errors, rerun with: -v
 ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)

【问题讨论】:

  • for (int i = 0; i != 10000; ++i) { free(malloc(1)); } 呢?
  • 如果您正在调用 C 库例程或正在链接任何库并调用它们,它们可以调用 malloc() 和 free()。
  • 在调试器下运行程序,在malloc()上设置断点。然后进行堆栈跟踪以查看它是从哪里调用的。
  • “我不能在这里发布任何代码,原因很明显”。这些原因并不那么明显,并且将其减少到一个有据可查的SSCCE 并为您的实现提供特定数字将使您的问题更加健壮,特别是如果所述示例具有与您的应用程序类似但最小化的工作流程。就目前而言,答案很明显:如果你没有调用这些函数,那么你调用的是某些函数。
  • “显而易见的原因”指的是我为工作编写的代码,而不是个人使用。我的工作不希望我为所有网络提供我们的代码。此外,答案显而易见。我非常清楚我正在调用 something,可能是通过 Charlie Burns 和 Pankrates 建议的 C 库间接调用的,valgrind 将其解释为“malloc”和“free”,但我不能告诉那是什么东西。因此,我问“是否还有其他 C 操作使 valgrind 计入 malloc 和 frees 的数量?”我的问题不需要示例代码。

标签: c malloc free valgrind


【解决方案1】:

好吧,如果您调用执行 malloc 和 free 调用的库函数,您将看到很多分配和释放。一些库函数、执行分配的系统调用是 strdup、pthread_create、timer_create、e.t.c

【讨论】:

    【解决方案2】:

    请记住,有许多函数调用可以分配内存、strdup、fopen、创建线程、加载共享对象、解析时区或语言环境信息(作为与时间相关的函数可能需要),3.d 方库可以在方式的数量,等等。

    但是,使用 valgrind massif 工具运行您的程序,http://valgrind.org/docs/manual/ms-manual.html(阅读那些文档)

    例如

     valgrind --depth=20  --tool=massif ./Calc
    

    这会生成一个 massif.out.XXX 文件,该文件向您显示各种分配来源,以及堆的快照,例如摘录:

    snapshot=9
    #-----------
    time=137984
    mem_heap_B=640
    mem_heap_extra_B=40
    mem_stacks_B=0
    heap_tree=peak
    n2: 640 (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
     n1: 352 0x4BFD095A: __fopen_internal (in /usr/lib/libc-2.17.so)
      n1: 352 0x4BFD0A39: fopen@@GLIBC_2.1 (in /usr/lib/libc-2.17.so)
       n0: 352 0x8048784: main (tailq_example.c:63)
     n5: 288 0x8048580: add_block (tailq_example.c:20)
      n0: 72 0x8048748: main (tailq_example.c:60)
      n0: 72 0x804875C: main (tailq_example.c:61)
      n0: 72 0x80487DC: main (tailq_example.c:72)
      n0: 72 0x80487F0: main (tailq_example.c:73)
      n0: 0 in 1 place, below massif's threshold (01.00%)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-02
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 2015-04-23
      • 2013-06-16
      相关资源
      最近更新 更多