分页: 1 / 1

linux下利用valgrind检测内存泄漏

发表于 : 2008-12-08 9:46
weihua2008
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吗?
知情者说两句.