内存泄漏检测工具valgrind

Valgrind是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合。
1 安装使用
1.1安装
#sudo apt-get install valgrind
1.2 命令介绍
用法:valgrind[options] prog-and-args [options]: 常用选项,适用于所有Valgrind工具
-tool= 最常用的选项。运行 valgrind中名为toolname的工具。默认memcheck。
h –help 显示帮助信息。
-version 显示valgrind内核的版本,每个工具都有各自的版本。
q –quiet 安静地运行,只打印错误信息。
v –verbose 更详细的信息, 增加错误数统计。
-trace-children=no|yes 跟踪子线程? [no]
-track-fds=no|yes 跟踪打开的文件描述?[no]
-time-stamp=no|yes 增加时间戳到LOG信息? [no]
-log-fd= 输出LOG到描述符文件 [2=stderr]
-log-file= 将输出的信息写入到filename.PID的文件里,PID是运行程序的进行ID
-log-file-exactly= 输出LOG信息到 file
-log-file-qualifier= 取得环境变量的值来做为输出信息的文件名。 [none]
-log-socket=ipaddr:port 输出LOG到socket ,ipaddr:port
LOG信息输出:
-xml=yes 将信息以xml格式输出,只有memcheck可用
-num-callers= show callers in stack traces [12]
-error-limit=no|yes 如果太多错误,则停止显示新错误? [yes]
-error-exitcode= 如果发现错误则返回错误代码 [0=disable]
-db-attach=no|yes 当出现错误,valgrind会自动启动调试器gdb。[no]
-db-command= 启动调试器的命令行选项[gdb -nw %f %p]
适用于Memcheck工具的相关选项:
-leak-check=no|summary|full 要求对leak给出详细信息? [summary]
-leak-resolution=low|med|high how much bt merging in leak check [low]
-show-reachable=no|yes show reachable blocks in leak check? [no]
2 应用实践
下面通过介绍几个范例来说明如何使用Memcheck (其他工具暂不涉及,感兴趣可以交流),示例仅供参考,更多用途可在实际应用中不断探索。
2.1数组越界/内存未释放
#include<stdlib.h>
void k(void)
{
int *x = malloc(8 * sizeof(int));
x[9] = 0; //数组下标越界
} //内存未释放

int main(void)
{
k();
return 0;
}
1)编译程序test.c
gcc -Wall test.c -g -o test#Wall提示所有告警,-g gdb,-o输出
2)使用Valgrind检查程序BUG
valgrind --tool=memcheck --leak-check=full ./test
#–leak-check=full 所有泄露检查
3) 运行结果如下:
2989 Memcheck, a memory error detector
2989 Copyright © 2002-2012, and GNU GPL’d, by Julian Seward
et al.
2989 Using Valgrind-3.8.1 and LibVEX; rerun with -h for
copyright info
2989 Command: ./test
2989
2989 Invalid write of size 4
2989 at 0x4004E2: k (test.c:5)
2989 by 0x4004F2: main (test.c:10)
2989 Address 0x4c27064 is 4 bytes after a block of size 32 alloc’d
2989 at 0x4A06A2E: malloc (vg_replace_malloc.c:270)
2989 by 0x4004D5: k (test.c:4)
2989 by 0x4004F2: main (test.c:10)
2989
2989
2989 HEAP SUMMARY:
2989 in use at exit: 32 bytes in 1 blocks
2989 total heap usage: 1 allocs, 0 frees, 32 bytes allocated
2989
2989 32 bytes in 1 blocks are definitely lost in loss record 1
of 1
2989 at 0x4A06A2E: malloc (vg_replace_malloc.c:270)
2989 by 0x4004D5: k (test.c:4)
2989 by 0x4004F2: main (test.c:10)
2989
2989 LEAK SUMMARY:
2989 definitely lost: 32 bytes in 1 blocks
2989 indirectly lost: 0 bytes in 0 blocks
2989 possibly lost: 0 bytes in 0 blocks
2989 still reachable: 0 bytes in 0 blocks
2989suppressed: 0 bytes in 0 blocks
2989
2989 For counts of detected and suppressed errors, rerun with: -v
2989 ERROR SUMMARY: 2 errors from 2 contexts
(suppressed: 6 from 6)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值