MemoryLeak Valgrind


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和解释错误的链接。

首先,如果你有泄漏,例如在C中,这意味着你做了一个malloc,但你没有释放它。使用man mallocman free获取更多信息。

我可以给你一个提示,当你启动Valgrind时,你可以使用参数--leak-check=full,它会显示你没有释放哪个malloc。

例如:

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

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

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

您可以通过创建"class obj = new class()"而不删除obj来运行快速测试,您肯定会在项目中创建内存泄漏。

最新更新