`
isiqi
  • 浏览: 16031872 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

调试内存

阅读更多
linux使用valgrind 工具检查内存泄露

Valgrind是一款非常强大的工具集合,它包含有包括内存检测、CPU监测等多种工具,其中最常用的是内存检测功能,它能监测出以下的各种内存错误:
1. 访问非法内存区域
2. 使用未被初始化的内存区域
3. 非法释放内存,比如多次free一个内存
4. 内存泄露

使用步骤:
1. 使用valgrind前需要使用-g参数编译源程序以便生成debug信息
2. 在运行程序的命令行前加上valgrind,例如:valgrind myprog argvlist
为了能够给出内存检测报告,需要加上--leak-check参数,那么上述命令就变成:valgrind --leak-check=yes myprog argvlist
这个时候程序运行会非常慢,消耗资源也会大幅增加,这是正常的,不必担心。因为valgrind需要收集内存错误和泄露的详细信息。这被有些人认为是valgrind的缺点,但是对于一个大项目来说,这一点时间的消耗来监测程序的稳定性是值得的。
3. 阅读valgrind给出的报告
valgrind的其它参数:
--num-callers=N:指定报告中调用栈的层数,这在定位和跟踪错误的时候会比较有用
-v :可以打印出更加详细的信息
valgrind的报告:
"Invalid write/read...":这一般是访问非法的内存区域,比如数组越界等
"Conditional jump or move depends on uninitialised value(s)":使用了未初始化的变量(包括未初始化的局部变量和未初始化的堆区域)
"Mismatched free() / delete / delete []":这一般是非法释法内存的错误,比如多次free一个内存
"LEAK SUMMARY":这表示下面是内存泄露的信息(造成内存丢失的程序行可以通过查看这一行前面的清单来定位)
"definitely lost":肯定丢失的部分,这种报告必须处理
"possibly lost":可能丢失的部分,这是由于C/C++语言指针处理的特点造成的,这部分可能不太准确
这里只有一个简单的介绍,更多详细信息参见valgrind的官方网站:http://valgrind.org/docs/manual/



Valgrind 是在linux系统下开发应用程序时用于调试内存问题的工具。它尤其擅长发现内存管理的问题,它可以检查程序运行时的内存泄漏问题。

它的官方网址是 http://www.valgrind.org/

下载最新版本的Valgrind,目前是3.2.0。 wget http://www.valgrind.org/downloads/valkyrie-1.2.0.tar.bz2

执行常规的安装步骤:./confgure && make && make install。注意: 系统必须安装QT的开发包。即便这样在make 时还是出现qplatformdefs.h这个文件找不到的情况,导致make失败。查找系统中的qplatformdefs.h 之后,发现没有存在于qt的标准头文件目录/usr/lib/qt-3.3/include。如是将/usr/lib/qt- 3.3/mkspecs/linux-g++/ 目录下该头文件复制标准头文件目录,重新make ,后面一切OK。

初次使用
    编译如下代码:  gcc -Wall example.c -g -o example 

#include <stdlib.h>

void f(void)
{
int* x = malloc(10 * sizeof(int));
x[10] = 0; // problem 1: heap block overrun
} // problem 2: memory leak -- x not freed

int main(void)
{
f();
return 0;
}

注意:gcc 的-g 选项让Valgrind调试输出时指出相应信息的代码所在的行号。

valgrind --tool=memcheck --leak-check=yes./example

==6742== Memcheck, a memory error detector for x86-linux.
==6742== Copyright (C) 2002-2004, and GNU GPL'd, by Julian Seward et al.
==6742== Using valgrind-2.2.0, a program supervision framework for x86-linux.
==6742== Copyright (C) 2000-2004, and GNU GPL'd, by Julian Seward et al.
==6742== For more details, rerun with: -v
==6742==
==6742== Invalid write of size 4
==6742== at 0x8048384: f (example.c:6)
==6742== by 0x80483AC: main (example.c:12)
==6742== Address 0x1B908050 is 0 bytes after a block of size 40 alloc'd
==6742== at 0x1B904984: malloc (vg_replace_malloc.c:131)
==6742== by 0x8048377: f (example.c:5)
==6742== by 0x80483AC: main (example.c:12)
==6742==
==6742== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 12 from 1)
==6742== malloc/free: in use at exit: 40 bytes in 1 blocks.
==6742== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
==6742== For counts of detected errors, rerun with: -v
==6742== searching for pointers to 1 not-freed blocks.
==6742== checked 1360800 bytes.
==6742==
==6742==
==6742== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==6742== at 0x1B904984: malloc (vg_replace_malloc.c:131)
==6742== by 0x8048377: f (example.c:5)
==6742== by 0x80483AC: main (example.c:12)
==6742==
==6742== LEAK SUMMARY:
==6742== definitely lost: 40 bytes in 1 blocks.
==6742== possibly lost: 0 bytes in 0 blocks.
==6742== still reachable: 0 bytes in 0 blocks.
==6742== suppressed: 0 bytes in 0 blocks.
==6742== Reachable blocks (those to which a pointer was found) are not shown.
==6742== To see them, rerun with: --show-reachable=yes

上面的C程序存在两个错误:1. 数组下标越界;2. 分配的内存没有释放,存在内存泄露的问题。对于错误1,看Valgrind的调试信息片断

==6742== Invalid write of size 4
==6742== at 0x8048384: f (example.c:6)
==6742== by 0x80483AC: main (example.c:12)
==6742== Address 0x1B908050 is 0 bytes after a block of size 40 alloc'd
==6742== at 0x1B904984: malloc (vg_replace_malloc.c:131)
==6742== by 0x8048377: f (example.c:5)

对于错误2,看这个

==6742== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.

......

==6742== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==6742== at 0x1B904984: malloc (vg_replace_malloc.c:131)
==6742== by 0x8048377: f (example.c:5)
==6742== by 0x80483AC: main (example.c:12)

相关链接:

http://www.valgrind.org/docs/manual/quick-start.html

http://www-128.ibm.com/developerworks/cn/linux/l-pow-debug/

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics