how to debug memory-leak

1: valgrind introduce
http://valgrind.org/docs/manual/mc-manual.html

2: make/run valgrind in host platform
- 列表内容
- 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

3: how to use valgrind
http://blog.csdn.net/21aspnet/article/details/8172124
http://blog.csdn.net/sduliulun/article/details/7732906
http://blog.chinaunix.net/uid-23629988-id-3033741.html

#include <stdlib.h>  
int* func(void)  
{  
   int* x = malloc(10 * sizeof(int));  
   x[10] = 0;  //问题1: 数组下标越界  
}                    
 int main(void)  
{  
   int* x=NULL;  
   x=func();  
   //free(x);    
   x=NULL;  
   return 0;   //问题2: 内存没有释放  
 }  

编译
$gcc -g -o test test.c

内存检查
$ valgrind –tool=memcheck –leak-check=yes –show-reachable=yes ./test

报告:

说明
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

4: references
http://blog.csdn.net/yangzhiloveyou/article/details/7935078
http://www.cnblogs.com/shuaihanhungry/p/5818713.html

cross compile valgrind
http://www.itdadao.com/articles/c15a376180p0.html
http://blog.csdn.net/roland_sun/article/details/46049485
https://gist.github.com/mnemonicflow/131eb830a4d74911bf5a
https://sergiolima.wordpress.com/2015/07/07/vallgrind-and-android-working-together/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值