valgrind使用

valgrind使用

 

1.前置条件

Compile your program with -g to include debugginginformation so that Memcheck's error messages include exact linenumbers. Using-O0 is also a goodidea, if you can tolerate the slowdown. With-O1 line numbers in error messages canbe inaccurate, although generally speaking running Memcheck on code compiledat-O1 works fairly well, and the speed improvementcompared to running-O0 is quite significant.Use of-O2 and above is not recommended asMemcheck occasionally reports uninitialised-value errors which don'treally exist.

使用valgrind检测的程序,在编译时候,需要加上 -g。尽量使用 -O0不进行代码编译优化。

 

 

2.检测内存泄漏:

1)编译程序:

用 -g 选项增加debug信息, -O0 或者 -O1 均可,-O0 运行非常缓慢,-O1 行号和错误信息会不准确。

不要使用-O2 或者更高的优化级别。

2)运行,检测是否有内存泄漏:

valgrind --leak-check=yes myprog arg1 arg2

用以下命令可以查看具体是哪个调用堆栈产生了内存泄漏(但运行速度缓慢):

valgrind --leak-check=full --show-leak-kinds=all myprog arg1 arg2

 

valgrind --leak-check=yes myprog arg1 arg2 

valgrind --leak-check=full --log-file=valgrind.log -v --track-origins=yes myprog arg1 arg2

堆栈不够大时加上--num-callers2:检测变量未赋初值--track-origins=yes

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值