linux内存调试、内存泄漏检测以及性能分析的工具-valgrind

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lilil371324/article/details/51155134

Valgrind这个名字取自北欧神话中英灵殿的入口。
Valgrind的最初作者是Julian Seward,他于2006年由于在开发Valgrind上的工作获得了第二届Google-O’Reilly开源代码奖。
Valgrind遵守GNU通用公共许可证条款,是一款自由软件。

官网链接

http://www.valgrind.org

下载与安装

下载地址:
http://www.valgrind.org/downloads/valgrind-3.11.0.tar.bz2
安装:

wget http://www.valgrind.org/downloads/valgrind-3.11.0.tar.bz2
cd valgrind-3.8.1
./configure --prefix=/usr/local/webserver/valgrind
make
make install

fedora 22 快速安装方式:

dnf install -y valgrind

试用:

编译

gcc -g -o test C.cpp

内存检查

valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./C

结果

[fedora@localhost C]$ valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./Debug/CTMPServer20160413
==2923== Memcheck, a memory error detector
==2923== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==2923== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==2923== Command: ./Debug/C
Read the configuration file 
==2923== Mismatched free() / delete / delete []
==2923==    at 0x4A08184: operator delete(void*) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==2923==    by 0x4054E3: readfile(int) (text.cc:156)
==2923==    by 0x40E7A0: global::parseFile(char*) (global.cc:238)
==2923==    by 0x40E165: global::global(char*, short, bool, char*, char*, char*) (global.cc:160)
==2923==    by 0x40F345: larbin(char*, short, bool, char*, char*, char*) (larbin.cc:69)
==2923==    by 0x40F6DE: main (C.cpp:25)
==2923==  Address 0x68e9b50 is 0 bytes inside a block of size 512 alloc'd
==2923==    by 0x40F6DE: main (C.cpp:25)
==2923== Invalid write of size 4
==2923==    at 0x406098: url::url(char*) (url.cc:219)
==2923==    by 0x4032E9: PersistentFifo::tryGet() (PersistentFifo.cc:98)

说明
Invalid write of size 4:表示数组越界写了4字节

文档:

下载地址
http://www.valgrind.org/docs/download_docs.html

Valgrind 中包含的 Memcheck 工具可以检查以下的程序错误:

  使用未初始化的内存 (Use of uninitialised memory)
  使用已经释放了的内存 (Reading/writing memory after it has been free’d)
  使用超过malloc分配的内存空间(Reading/writing off the end of malloc’d blocks)
  对堆栈的非法访问 (Reading/writing inappropriate areas on the stack)
  申请的空间是否有释放 (Memory leaks – where pointers to malloc’d blocks are lost forever)
  malloc/free/new/delete申请和释放内存的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete [])
  src和dst的重叠(Overlapping src and dst pointers in memcpy() and related functions)
  重复free
其他情况还不清楚,等待后续添加

扫描二维码关注公众号,回复: 4285359 查看本文章

猜你喜欢

转载自blog.csdn.net/lilil371324/article/details/51155134