valgrind 将哪些 C 操作视为'malloc'和"自由"?



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

这是我的瓦尔格林德报告。 如您所见,从这个角度来看,一切看起来都不错。

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)

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

请记住,有许多函数调用可以分配内存、strdup、fopen、创建线程、加载共享对象、解析时区或区域设置信息(因为与时间相关的函数可能需要(,3.d 派对库可以通过多种方式分配内存,等等。

但是,使用valgrind地块工具运行您的程序,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%)

最新更新