当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 1 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : linux下利用valgrind检测内存泄漏
帖子发表于 : 2008-12-08 9:46 

注册: 2008-07-10 15:08
帖子: 448
送出感谢: 0 次
接收感谢: 0 次
1.首先我安装了valgrind.
2.编写一个存在内存泄漏的程序:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void fun()
{
int *p=(int*)malloc(10*sizeof(int));
p[0]=0;
}
int main(int argc,char *argv[])
{
fun();
return 0;
}
3.编译:gcc -Wall -g sample.c -o sample
4.利用vagrind ./ww检测是否有内存泄漏

终端输出信息为weihua@weihua:~/learn/valgrind$ valgrind ./ww
==5681== Memcheck, a memory error detector.
==5681== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==5681== Using LibVEX rev 1732, a library for dynamic binary translation.
==5681== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==5681== Using valgrind-3.2.3-Debian, a dynamic binary instrumentation framework.
==5681== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==5681== For more details, rerun with: -v
==5681==
==5681==
==5681== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from 1)
==5681== malloc/free: in use at exit: 40 bytes in 1 blocks.
==5681== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
==5681== For counts of detected errors, rerun with: -v
==5681== searching for pointers to 1 not-freed blocks.
==5681== checked 59,820 bytes.
==5681==
==5681== LEAK SUMMARY:
==5681== definitely lost: 40 bytes in 1 blocks.
==5681== possibly lost: 0 bytes in 0 blocks.
==5681== still reachable: 0 bytes in 0 blocks.
==5681== suppressed: 0 bytes in 0 blocks.
==5681== Rerun with --leak-check=full to see details of leaked memory.
然而网上的例子按照我的方法来运行的话在终端输出的是:
==32372== Memcheck, a memory error detector.
==32372== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==32372== Using LibVEX rev 1854, a library for dynamic binary translation.
==32372== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==32372== Using valgrind-3.2.3-Debian, a dynamic binary instrumentation framework.
==32372== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==32372== For more details, rerun with: -v
==32372==
==32372==
==32372==Invalid write of size 4
==32372== at 0x80483CF: fun (sample.c: 6)
==32372== by 0x80483EC: main (sample.c: 11)
==32372==Address 0x416f050 is 0 bytes after a block of size 40 alloc'd
==32372== at 0x4023888: malloc (vg_replace_malloc.c:207)
==32372== by 0x80483C5: fun (sample.c: 5)
==32372== by 0x80483EC: main (sample.c: 11)
==32372== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 3 from 1)
==32372== malloc/free: in use at exit: 40 bytes in 1 blocks.
==32372== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
==32372== For counts of detected errors, rerun with: -v
==32372== searching for pointers to 1 not-freed blocks.
==32372== checked 56,780 bytes.
==32372==
==32372== LEAK SUMMARY:
==32372== definitely lost: 40 bytes in 1 blocks.
==32372== possibly lost: 0 bytes in 0 blocks.
==32372== still reachable: 0 bytes in 0 blocks.
==32372== suppressed: 0 bytes in 0 blocks.
==32372== Rerun with --leak-check=full to see details of leaked memory.
是我的valgrind安装错了还是撒的?valgrind一开始不就是默认的Memcheck吗?
知情者说两句.


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 1 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 2 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译