c程序内存泄露检查工具
来源:互联网 发布:linux设置开机启动程序 编辑:程序博客网 时间:2024/04/29 17:28
Valgrind是一款用于内存调试、内存泄漏检测以及性能分析的软件开发工具。
Valgrind的最初作者是Julian Seward,他于2006年由于在开发Valgrind上的工作获得了第二届Google-O'Reilly开源代码奖。
Valgrind遵守GNU通用公共许可证条款,是一款自由软件。
官网
http://www.valgrind.org
下载与安装
#wget http://www.valgrind.org/downloads/valgrind-3.8.1.tar.bz2
#tar xvf valgrind-3.8.1.tar.bz2
#cd valgrind-3.8.1
#./configure --prefix=/usr/local/webserver/valgrind
#make
#make install
测试代码
#include <stdlib.h>
int* func(void)
{
int* x = malloc(10 * sizeof(int));
x[10] = 0;
}
int main(void)
{
int* x=NULL;
x=func();
//free(x);
x=NULL;
return 0;
}
编译
#gcc -g -o test test.c
内存检查
/usr/local/webserver/valgrind/bin/valgrind --tool=memcheck --leak-check=yes --show-reachable=yes /data_disk/webdata/c/test
检查结果
==27394== Memcheck, a memory error detector
==27394== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==27394== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==27394== Command: /data_disk/webdata/c/test
==27394==
==27394== Invalid write of size 4
==27394== at 0x4004E2: func (test.c:5)
==27394== by 0x4004FE: main (test.c:10)
==27394== Address 0x51c2068 is 0 bytes after a block of size 40 alloc'd
==27394== at 0x4C278FE: malloc (vg_replace_malloc.c:270)
==27394== by 0x4004D5: func (test.c:4)
==27394== by 0x4004FE: main (test.c:10)
==27394==
==27394==
==27394== HEAP SUMMARY:
==27394== in use at exit: 40 bytes in 1 blocks
==27394== total heap usage: 1 allocs, 0 frees, 40 bytes allocated
==27394==
==27394== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==27394== at 0x4C278FE: malloc (vg_replace_malloc.c:270)
==27394== by 0x4004D5: func (test.c:4)
==27394== by 0x4004FE: main (test.c:10)
==27394==
==27394== LEAK SUMMARY:
==27394== definitely lost: 40 bytes in 1 blocks
==27394== indirectly lost: 0 bytes in 0 blocks
==27394== possibly lost: 0 bytes in 0 blocks
==27394== still reachable: 0 bytes in 0 blocks
==27394== suppressed: 0 bytes in 0 blocks
==27394==
==27394== For counts of detected and suppressed errors, rerun with: -v
==27394== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 6 from 6)
说明
Invalid write of size 4:表示数组越界写了4字节
40 bytes in 1 blocks:表示因程序退出而发生内存泄露40字节
文档:
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
- c程序内存泄露检查工具
- Linux下几款C++程序中的内存泄露检查工具
- Linux下几款C++程序中的内存泄露检查工具
- Linux下几款C++程序中的内存泄露检查工具
- Linux下几款C++程序中的内存泄露检查工具
- 检查内存泄露的工具
- C内存泄露检查工具:Visual Leak Detector使用
- 【C/C++ 语法备忘】2、内存泄露检查工具VLD
- Win32程序检查内存泄露
- MFC程序内存泄露检查
- 如何使用Valgrind内存检查工具 检查C/C++中内存泄露
- 【Android】【内存】内存泄露检查工具 LeakCanary
- 如何检查程序内存泄露?如果不用工具,不用code review,如何检查?
- linux使用valgrind 工具检查内存泄露
- 内存泄露检查工具及相关网站
- Visual studio内存泄露检查工具--BoundsChecker
- 简单的内存泄露检查工具
- Linux 内存泄露检查工具valgrind
- cocos2d-x在android上显示段落文字,并且实现空格和下划线,用到scrollview
- Couting the Pixels with Histograms
- Cocos2d-x设计模式发掘之二:二段构建模式
- 14-HEVC中的PPS解析
- mapreduce编程(二)- 大象书中求每一年的最高温度
- c程序内存泄露检查工具
- Cocosd-x设计模式之三:管理者模式
- 【九度】排序整理
- Cocosd-x设计模式之四 :外观模式
- JAVA线程学习笔记(三)---线程同步(一)
- Cocosd-x设计模式之五 :防御式编程模式
- POJ_2485 Highway_Kruskal
- 15-HEVC中的图像的分块分割方法
- Android Service学习之AIDL, Parcelable和远程服务