-
Valgrind是什么?
Valgrind是一个提供程序调试及性能分析的工具集。其包含的工具主要有Memcheck,Cachegrind,Callgrind,Massif等。其中,最为常用的是Memcheck,其主要用来检查程序heap上的内存使用情况。本文档主要介绍Memcheck的用法和一些使用技巧。
其官方网站是:http://valgrind.org/
-
Valgrind能干什么不能干什么?
Valgrind主要用来检查程序中可能出现的以下问题:
-
Use of uninitialised memory
-
Reading/writing memory after it has been free’d
-
Reading/writing off the end of malloc’d block
-
Memory leaks -- where pointers to malloc’d blocks are lost foreve
-
Mismatched use of malloc/new/new [] vs free/delete/delete []
-
Overlapping src and dst pointers in memcpy() and related functions
其功能约束如下:
-
只能检查heap上的错误,不能检查出static和stack内存的使用,如数组越界等。
- 不能指出为什么泄漏,也不能指出在哪内存泄漏
-
指出的错误并非100%正确,但建议在编译时至少以warning的心态对待它们。
-
Valgrind的安装与部署
若有root权限,其安装方式如下:
-
从官网上下载valgrind安装包:http://valgrind.org/downloads/valgrind-3.3.0.tar.bz2
-
用bzip2及tar命令解压压缩包。
-
进入解压目录,运行./configure
-
运行“make”命令
-
运行“make install”命令
-
运行“valgrind ls- l”测试valgrind是否已经正确安装到计算机上。若正确安装,则会出现类似第四部分的报错信息。
若没有root权限,则在第3步时,可以用--prefix指定安装的目录
./configure–prefix=/home/work/yangfenqiang/
以下步骤相同。
-
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.
其中:
-
==2014==表示进程号信息,基本没用。
-
接下来是Memcheck的版权声明信息。
-
详细的报错信息,如at 0x4009C6: main (test.cpp:7)
Address 0x4a2005c is 4 bytes after a block of size 40 alloc'd
说明test.cpp的第7行发生内存访问越界,越界的位移为4。
-
ERROR SUMMARY下面为错误汇总信息。
-
接着是内存泄漏信息。说明有40byte的内存泄漏。
-
LEAK SUMMARY为内存泄漏信息。
在LEAK SUMMARY中:
-
definitely lost:表明没有任何指针指向该区域,已经造成了内存泄漏。
-
possibly lost:存在指针指向内存中的某个位置,valgrind认为你有可能是在做一些其他的高级应用(将指针放在申请的内存块中间)
-
still reachable:仍有指针引用该内存块,只是没有释放而已,可以通过设置—show-reachable=yes来报错。
-
Valgrind常用命令参数
-
--tool=<name> [default=memcheck]
--tool参数指明所要使用valgrind的哪一个工具,默认的为memcheck。因为大多数情况下我们只会用到memcheck工具,因此该参数可以不写。
-
--leak-check=<no|summary|yes|full>[default:summary]
在退出时检查是否有泄漏。Summary只是告诉我们有多少次泄漏,yes或full会告诉我们每次泄漏的详细信息。
-
--show-reachable=<yes|no>[default:no]
通过设定该参数为yes,则显示still
reachable类型的内存泄漏信息。
其他更多的运行参数信息可以查看《valgrind使用指南》及《valgrind
manual》。
分享到:
相关推荐
valgrind-arm64.zip 预编译好的valgrind for android aarch64。 可参考下面链接跳过编译部分,直接安装使用。 https://blog.csdn.net/yaxf999/article/details/53749184 valgrind内存检测工具使用方法: adb push .\...
valgrind是一款运行在linux下的,用来定位c/c++程序中内存使用方面的错误的工具,包括:内存泄漏、使用未初始化的内存、读/写已释放的内存、读/写内存越界、使用malloc/new/new[]和free/delete/delete[]不匹配,等等...
内存检查工具valgrind 的使用方法,通过valgrind 可以检查程序中的内存情况。
valgrind安装包 安装步骤 安装说明;以及一些检查内存泄漏的用法
预编译好的valgrind for android armeabi-v7a。 可参考下面链接跳过编译部分,直接安装使用。 https://blog.csdn.net/yaxf999/article/details/53749184 adb push valgrind-arm/Inst\data/local/Inst /data/local...
亲测可以正常使用的valgrind在android板子上使用,对于需要用valgrind检测内存泄漏等问题的开发者有参考价值。
valgrind arm64交叉编译包,可以直接运行,里面有安装说明。 附带解决valgrind运行时报错“Note that if you are debugging a 32 bit process on a 64 bit system...”的依赖库:dpkg -i libc6_2.31-13+deb11u5_arm...
valgrind-3.19.0 内存检测工具 aarch64 6.5.0交叉编译移植版本
Valgrind的简介和使用方法,介绍了Valgrind的使用以及如何进行程序检查
valgrind for android NDK, 由国外相关资源整理,并亲测试可用,只支持 arm 架构.
valgrind
Valgrind是一款用于内存调试、内存泄漏检测以及性能分析的软件开发工具。Valgrind这个名字取自北欧神话中英灵殿的入口。 Valgrind的最初作者是Julian Seward,他于2006年由于在开发Valgrind上的工作获得了第二届...
arm-linux-gnueabihf-valgrind-3.15.0 arm-linux-gnueabihf端使用
valgrind,内存检查工具,用于内存泄漏的检查
valgrind 2017年版的最新手册manual. 并且带有书签. 阅读文档比博客收获更多.
valgrind的使用方法-详细手册
valgrind安装与使用
压缩包为valgrind-2.13.0源码,需要的同学自行下载,也可以到官网下载,官网可能会有更新的版本。
Valgrind tool for cross compilation on armv6.