valgrind基本使用

1。valgrind是一个内存检测工具,类似的还有purify,insure++等

2。测试文件test.c

  test.c :

    main(){

      int* a=new int[100];

      return 0;

    }

  第一步:gcc -o test test.c

  第二步: valgrind --tool=memcheck --leak-check=yes ./test

  结果:

==3053== Memcheck, a memory error detector
==3053== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==3053== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==3053== Command: ./a.out
==3053==
==3053==
==3053== HEAP SUMMARY:
==3053== in use at exit: 400 bytes in 1 blocks
==3053== total heap usage: 1 allocs, 0 frees, 400 bytes allocated
==3053==
==3053== 400 bytes in 1 blocks are definitely lost in loss record 1 of 1
==3053== at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3053== by 0x8048528: main (temp.cc:41)
==3053==
==3053== LEAK SUMMARY:
==3053== definitely lost: 400 bytes in 1 blocks
==3053== indirectly lost: 0 bytes in 0 blocks
==3053== possibly lost: 0 bytes in 0 blocks
==3053== still reachable: 0 bytes in 0 blocks
==3053== suppressed: 0 bytes in 0 blocks
==3053==
==3053== For counts of detected and suppressed errors, rerun with: -v
==3053== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

分析:

  上面的==3053==,3053代表test的进程号

  从上面可看到 ,by 0x8048528:main(temp.cc:41)第41行用operator new分配了400字节内存但是没有释放,最后总结此进程泄露内存400字节

3.将内存检测工具与gdb一起使用

  命令: valgrind --tool=memcheck --leak-check=yes --db-attach=yes ./test

  --db-attach默认调试器是GDB,可以使用--db-attach=<command>指定

posted on 2013-06-22 20:19  紫金树下  阅读(279)  评论(0编辑  收藏  举报