2024年10月Linux系统中怎么使用valgrind检查内存(2)

发布时间:

  ⑴执行valgrind来检测内存错误:

  ⑵valgrind --track-fds=yes --leak-check=full --undef-value-errors=yes 。/a.out

  ⑶上面那些option的具体含义,可以参加valgrind --help,其中有些option默认就是打开的,不过我习惯于明确的使用option,以示清晰。

  ⑷==== Memcheck, a memory error detector

  ⑸==== Copyright (C -, and GNU GPL‘d, by Julian Seward et al.

  ⑹==== Using Valgrind-.. and LibVEX; rerun with -h for copyright info

  ⑺==== mand: 。/a.out

  ⑻/* 这里检测到了动态内存的越界,提示Invalid write。*/

  ⑼==== Invalid write of size

  ⑽==== at xB: mem_overrun (in /home/fgao/works/test/a.out

  ⑾==== by x: main (in /home/fgao/works/test/a.out

  ⑿==== Address xf is bytes inside a block of size alloc’d

  ⒀==== at xBDC: malloc (vg_replace_malloc.c:

  ⒁==== by xAD: mem_overrun (in /home/fgao/works/test/a.out

  ⒂==== by x: main (in /home/fgao/works/test/a.out

  ⒃/* 这里检测到了double free问题,提示Invalid Free */

  ⒄==== Invalid free( / delete / delete[]

  ⒅==== at xF: free (vg_replace_malloc.c:

  ⒆==== by x: mem_double_free (in /home/fgao/works/test/a.out

  ⒇==== by xD: main (in /home/fgao/works/test/a.out

  ⒈==== Address x is bytes inside a block of size free‘d

  ⒉==== at xF: free (vg_replace_malloc.c:

  ⒊==== by x: mem_double_free (in /home/fgao/works/test/a.out

  ⒋==== by xD: main (in /home/fgao/works/test/a.out

  ⒌/* 这里检测到了未初始化变量 */

  ⒍==== Conditional jump or move depends on uninitialised value(s

  ⒎==== at xB: free (vg_replace_malloc.c:

  ⒏==== by xC: mem_free_wild_pointer (in /home/fgao/works/test/a.out

  ⒐==== by x: main (in /home/fgao/works/test/a.out

  ⒑/* 这里检测到了非法是否野指针 */

  ⒒==== Invalid free( / delete / delete[]

  ⒓==== at xF: free (vg_replace_malloc.c:

  ⒔==== by xC: mem_free_wild_pointer (in /home/fgao/works/test/a.out

  ⒕==== by x: main (in /home/fgao/works/test/a.out

  ⒖==== Address x is bytes inside a block of size free’d

  ⒗==== at xF: free (vg_replace_malloc.c:

  ⒘==== by x: mem_double_free (in /home/fgao/works/test/a.out

  ⒙==== by xD: main (in /home/fgao/works/test/a.out

  ⒚这里检测到了文件指针资源的泄露,下面提示说有个文件描述符在退出时仍是打开的。

  ⒛描述符,,无需关心,通过报告,可以发现程序中自己明确打开的文件描述符没有关闭。

  ①==== FILE DESCRIPTORS: open at exit.

  ②==== Open file descriptor : test.txt

  ③==== at xD: __open_nocancel (in /lib/libc-..so

  ④==== by xFEC: __fopen_internal (in /lib/libc-..so

  ⑤==== by xFB: fopenGLIBC_. (in /lib/libc-..so

  ⑥==== by x: mem_leak (in /home/fgao/works/test/a.out

  ⑦==== by xE: main (in /home/fgao/works/test/a.out

  ⑧==== Open file descriptor : /dev/pts/

  ⑨==== Open file descriptor : /dev/pts/

  ⑩==== Open file descriptor : /dev/pts/

  Ⅰ/* 堆信息的总结:一共调用次alloc,次free。之所以正好相等,因为上面有一函数少了free,有一个函数多了一个free */

  Ⅱ==== HEAP SUMMARY:

  Ⅲ==== in use at exit: bytes in blocks

  Ⅳ==== total heap usage: allocs, frees, bytes allocated

  Ⅴ/* 检测到一个字节的内存泄露 */

  Ⅵ==== bytes in blocks are definitely lost in loss record of

  Ⅶ==== at xBDC: malloc (vg_replace_malloc.c:

  Ⅷ==== by x: mem_leak (in /home/fgao/works/test/a.out

  Ⅸ==== by x: main (in /home/fgao/works/test/a.out

  Ⅹ/* 内存泄露的总结 */

  ㈠==== LEAK SUMMARY:

  ㈡==== definitely lost: bytes in blocks

  ㈢==== indirectly lost: bytes in blocks

  ㈣==== possibly lost: bytes in blocks

  ㈤==== still reachable: bytes in blocks

  ㈥==== suppressed: bytes in blocks

  ㈦==== Reachable blocks (those to which a pointer was found are not shown.

  ㈧==== To see them, rerun with: --leak-check=full --show-reachable=yes

  ㈨==== For counts of detected and suppressed errors, rerun with: -v

  ㈩==== Use --track-origins=yes to see where uninitialised values e from

  ==== ERROR SUMMARY: errors from contexts (suppressed: from

  这个只是一个简单的示例程序,即使没有Valgrind,我们也可以很轻易的发现问题。但是在真实的项目中,当代码量达到万行,十万行,甚至百 万行时。由于申请的内存可能不是在一个地方使用,不可避免的被传来传去。这时,如果光是看review代码来检查问题,可能很难找到根本原因。这时,使用 Valgrind则可以很容易的发现问题所在。

  以上就是Linux系统中怎么使用valgrind检查内存的介绍了,当然还有一些错误时valgrind检查不到的,用户应该尽量避免出现太多的错误。