1. Valgrind是什么?
Valgrind是一个提供程序调试及性能分析的工具集。其包含的工具主要有MemcheckCachegrindCallgrindMassif等。其中,最为常用的是Memcheck,其主要用来检查程序heap上的内存使用情况。本文档主要介绍Memcheck的用法和一些使用技巧。
其是:    /
2. Valgrind能干什么不能干什么?
Valgrind主要用来检查程序中可能出现的以下问题:
1. Use of uninitialised memory
2. Reading/writing memory after it has been free’d
3. Reading/writing off the end of malloc’d block
4. Memory leaks -- where pointers to malloc’d blocks are lost foreve
5. Mismatched use of malloc/new/new [] vs free/delete/delete []
6. Overlapping src and dst pointers in memcpy() and related functions
功能约束如下:
1. 只能检查heap上的错误,不能检查出staticstack内存的使用,如数组越界等。
2. 不能指出为什么泄漏,也不能指出在哪内存泄漏
3. 指出的错误并非100%正确,但建议在编译时至少以warning的心态对待它们。
3. Valgrind的安装与部署
若有root权限,其安装方式如下:
1. 从上下载delete invalgrind 安装包:/downloads/valgrind-3.3.0.tar.bz2
2. bzip2tar命令解压压缩包。
3. 进入解压目录,运行./configure
4. 运行“make”命令
5. 运行“make install”命令
6. 运行“valgrind ls- l”测试valgrind是否已经正确安装到计算机上。若正确安装,则会出现类似第四部分的报错信息。
若没有root权限,则在第3步时,可以用--prefix指定安装的目录
./configure –prefix= /home/work/yangfenqiang/
以下步骤相同。
4. Valgrind使用示例及报错信息说明
编写程序test.cpp如下:
      1 #include <iostream>
      2 using namespace std;
      3
      4 int main()
      5 {
      6    int *a = new int[10];
      7    a[11] = 0;
      8    cout << a[11]<< endl;
      9    return 0;
    10 }
    11
编译该程序:gcc –g –o test test.cpp
注意加入-g参数,便于valgrind读入符号表之类的信息以提供更丰富的错误定位信息。不推荐加入-O等优化参数,因为优化后的代码易于让valgrind解释错误。
运行“valgrind --tool=memcheck --leak-check=yes --show-reachable=yes test”,显示如下信息:
==2051== Memcheck, a memory error detector.
==2051== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==2051== Using LibVEX rev 1804, a library for dynamic binary translation.
==2051== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==2051== Using valgrind-3.3.0, a dynamic binary instrumentation framework.
==2051== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==2051== For more details, rerun with: -v
==2051==
==2051== Invalid write of size 4
==2051==    at 0x4009C6: main (test.cpp:7)
==2051==  Address 0x4a2005c is 4 bytes after a block of size 40 alloc'd
==2051==    at 0x490581B: operator new[](unsigned long) (vg_replace_malloc.c:274)
==2051==    by 0x4009B9: main (test.cpp:6)
==2051==
==2051== Invalid read of size 4
==2051==    at 0x4009D4: main (test.cpp:8)
==2051==  Address 0x4a2005c is 4 bytes after a block of size 40 alloc'd
==2051==    at 0x490581B: operator new[](unsigned long) (vg_replace_malloc.c:274)
==2051==    by 0x4009B9: main (test.cpp:6)
0
==2051==
==2051== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 9 from 4)
==2051== malloc/free: in use at exit: 40 bytes in 1 blocks.
==2051== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
==2051== For counts of detected errors, rerun with: -v
==2051== searching for pointers to 1 not-freed blocks.
==2051== checked 198,560 bytes.
==2051==
==2051==
==2051== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==2051==    at 0x490581B: operator new[](unsigned long) (vg_replace_malloc.c:274)
==2051==    by 0x4009B9: main (test.cpp:6)
==2051==
==2051== LEAK SUMMARY:
==2051==    definitely lost: 40 bytes in 1 blocks.
==2051==      possibly lost: 0 bytes in 0 blocks.
==2051==    still reachable: 0 bytes in 0 blocks.
==2051==        suppressed: 0 bytes in 0 blocks.
其中:
1. ==2014== 表示进程号信息,基本没用。
2. 接下来是Memcheck的版权声明信息。
3. 详细的报错信息,如at 0x4009C6: main (test.cpp:7) Address 0x4a2005c is 4 bytes after a block of size 40 alloc'd
说明test.cpp的第7行发生内存访问越界,越界的位移为4
4. ERROR SUMMARY下面为错误汇总信息。
5. 接着是内存泄漏信息。说明有40byte的内存泄漏。
6. LEAK SUMMARY为内存泄漏信息。
LEAK SUMMARY中:
definitely lost:表明没有任何指针指向该区域,已经造成了内存泄漏。
possibly lost:存在指针指向内存中的某个位置,valgrind认为你有可能是在做一些其他的高级应用(将指针放在申请的内存块中间)
still reachable:仍有指针引用该内存块,只是没有释放而已,可以通过设置—show-reachable=yes来报错。
5. Valgrind常用命令参数
1. --tool=<name> [default=memcheck]
--tool参数指明所要使用valgrind的哪一个工具,默认的为memcheck。因为大多数情况下我们只会用到memcheck工具,因此该参数可以不写。
2. --leak-check=<no|summary|yes|full>[default:summary]
在退出时检查是否有泄漏。Summary只是告诉我们有多少次泄漏,yesfull会告诉我们每次泄漏的详细信息。
3. --show-reachable=<yes|no>[default:no]
通过设定该参数为yes,则显示still reachable类型的内存泄漏信息。
其他更多的运行参数信息可以查看《valgrind使用指南》及《valgrind manual》。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。