MemoryLeak Valgrind

时间:2015-10-03 21:15:10

标签: linux

I am using valgrind for the first time in linux 

运行可执行文件时收到以下消息。

      ==31440==
      ==31440== FILE DESCRIPTORS: 3 open at exit.
      ==31440== Open file descriptor 2: /dev/pts/9
      ==31440==    <inherited from parent>
      ==31440==
      ==31440== Open file descriptor 1: /dev/pts/9
      ==31440==    <inherited from parent>
      ==31440==
      ==31440== Open file descriptor 0: /dev/pts/9
      ==31440==    <inherited from parent>
      ==31440==
      ==31440==
      ==31440== HEAP SUMMARY:
      ==31440==     in use at exit: 72,704 bytes in 1 blocks
      ==31440==   total heap usage: 40 allocs, 39 frees, 91,192 bytes allocated
      ==31440==
      ==31440== 72,704 bytes in 1 blocks are still reachable in loss record 1 of     1
      ==31440==    at 0x4C277AB: malloc (in    
     /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==31440==    by 0x4EC3AAF: _GLOBAL__sub_I_eh_alloc.cc (eh_alloc.cc:117)
    ==31440==    by 0x400E859: call_init.part.0 (in /lib64/ld-2.18.so)
     ==31440==    by 0x400E942: _dl_init (in /lib64/ld-2.18.so)
     ==31440==    by 0x40011C9: ??? (in /lib64/ld-2.18.so)
     ==31440==
     ==31440== LEAK SUMMARY:
     ==31440==    definitely lost: 0 bytes in 0 blocks
     ==31440==    indirectly lost: 0 bytes in 0 blocks
      ==31440==      possibly lost: 0 bytes in 0 blocks
      ==31440==    still reachable: 72,704 bytes in 1 blocks
      ==31440==         suppressed: 0 bytes in 0 blocks
      ==31440==
      ==31440== For counts of detected and suppressed errors, rerun with: -v
       ==31440== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)

这是否意味着我有内存泄漏?我怎么知道是否有内存泄漏。 你们中的任何人都可以建议我如何使用valgrind并解释错误。提前谢谢。

2 个答案:

答案 0 :(得分:1)

首先,如果您在C中有泄漏,则意味着您执行了malloc,但是您没有释放它。使用man mallocman free获取更多信息。

我可以在您启动Valgrind时给您一个提示,您可以使用参数--leak-check=full,它会告诉您哪些malloc没有免费。

例如:

$>valgrind --leak-check=full ./foo.

答案 1 :(得分:0)

如果您的程序有内存泄漏,它将在LEAK SUMMARY中指定有多少行存在内存泄漏。

 definitely lost: x bytes in 0 blocks
 indirectly lost: y bytes in 0 blocks

您可以通过创建&#34;类obj = new class()&#34;来运行快速测试。在不删除obj的情况下,您肯定会在项目中创建内存泄漏。