快速业务通道

防止内存泄露 Linux下用Valgrind做检查

作者 佚名技术 来源 Linux系统 浏览 发布时间 2012-04-18
链很清晰,这样可以减少你在浏览大型C 程序时的混乱.比如在使用这个选项的时候,用memcheck检查openoffice就很容易.当然,你可能不会做这项工作,但是使用这一选项valgrind生成更精确的错误报告和减少混乱.

  一些编译优化选项(比如-O2或者更高的优化选项),可能会memcheck提交错误的未初始化报告,因此,为了valgrind的报告更精确,在编译的时候最好不要使用优化选项. 拥有帝国一切,皆有可能。欢迎访问phome.net

  如果程序是通过脚本启动的,可以修改脚本里启动程序的代码,或者使用--trace-children=yes选项来运行脚本.

  下面是用memcheck检查ls -l命令的输出报告,在终端下执行下面的命令

  valgrind --tool=memcheck ls -l

  程序会打印出ls -l命令的结果,是valgrind的检查报告如下:

  ==4187==

  ==4187== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 19 from 2)

  ==4187== malloc/free: in use at exit: 15,154 bytes in 105 blocks.

  ==4187== malloc/free: 310 allocs, 205 frees, 60,093 bytes allocated.

  ==4187== For counts of detected errors, rerun with: -v

  ==4187== searching for pointers to 105 not-freed blocks.

  ==4187== checked 145,292 bytes.

  ==4187==

  ==4187== LEAK SUMMARY:

  ==4187== definitely lost: 0 bytes in 0 blocks.

  ==4187== possibly lost: 0 bytes in 0 blocks.

  ==4187== still reachable: 15,154 bytes in 105 blocks.

  ==4187== suppressed: 0 bytes in 0 blocks.

  ==4187== Reachable blocks (those to which a pointer was found) are not shown.

  ==4187== To see them, rerun with: --show-reachable=yes

  这里的“4187”指的是执行ls -l的进程ID,这有利于区别不同进程的报告.memcheck会给出报告,分配置和释放了多少内存,有多少内存泄漏了,还有多少内存的访问是可达的,检查了多少字节的内存.

  下面举两个用valgrind做内存检查的例子

  例子一 (test.c):

  #include <string.h>

  int main(int argc, char *argv[])

  {

  char *ptr;

  ptr = (char*) malloc(10);

  strcpy(ptr, "01234567890");

  return 0;

  }

  编译程序

  gcc -g -o test test.c

  用valgrind执行命令

  valgrind --tool=memcheck --leak-check=yes ./test

  报告如下

  ==4270== Memcheck, a memory error detector.

  ==4270== Copyright (C) 2002-2006, and GNU GPL''d, by Julian Seward et al.

  ==4270== Using LibVEX rev 1606, a library for dynamic binary translation.

  ==4270== Copyright (C) 2004-2006, and GNU GPL''d, by OpenWorks LLP. 拥有帝国一切,皆有可能。欢迎访问phome.net

  ==4270== Using valgrind-3.2.0, a dynamic binary instrumentation framework.

  ==4270== Copyright (C) 2000-2006, and GNU GPL''d, by Julian Seward et al.

  ==4270== For more details, rerun with: -v

  ==4270==

  ==4270== Invalid write of size 1

  ==4270== at 0x4006190: strcpy (mc_replace_strmem.c:271)

  ==4270== by 0x80483DB: main (test.c:8)

  ==4270== Address 0x4023032 is 0 bytes after a block of size 10 alloc''d

  ==4270== at 0x40044F6: malloc (vg_replace_malloc.c:149)

  ==4270== by 0x80483C5: main (test.c:7)

  ==4270==

  ==4270== Invalid write of size 1

  ==4270== at 0x400619C: strcpy (mc_replace_strmem.c:271)

  ==4270== by 0x8048

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号