介绍一下内存泄漏工具valgrind的使用

valgrind

是一款开源内存检测的工具,里面包含了很多的工具,一般使用Memcheck这个工具偏多;

Memcheck:这是valgrind应用最广泛的工具,一个重量级的内存检查器,能够发现开发中绝大多数内存错误使用情况,比如:使用未初始化的内存,使用已经释放了的内存,内存访问越界等。

注:代码编译时我们要带上参数 -g,有了符号表才可以显示行号这些东西;

 

valgrind工具命令参考:

valgrind --tool=memcheck --leak-check=full --show-reachable=yes --trace-children=yes ./test

其中–leak-check=full 指的是完全检查内存泄漏,

–show-reachable=yes是显示内存泄漏的地点,

–trace-children=yes是跟入子进程。

 

测试代码及演示如下:

t1.c:

#include <stdio.h>
#include <stdlib.h>

#include "t1.h"

void func1(void)
{
	char *ptr1;
	ptr1 = malloc(512);
}

t1.h:

#ifndef _T1_H
#define _T1_H

void func1(void);

#endif

t2.c:

#include <stdio.h>
#include <stdlib.h>

#include "t2.h"

void func3(void)
{
	char *ptr3;
	ptr3 = malloc(1280);
}

void func2(void)
{
	char *ptr2;
	ptr2 = malloc(2048);
	func3();
}

t2.h:

#ifndef _T2_H
#define _T2_H

void func2(void);

#endif

main.c:

#include <stdio.h>
#include <stdlib.h>

#include "t1.h"
#include "t2.h"

void main(int argc, char *argv[])
{
	char *ptr0;
	ptr0 = malloc(200);
	func1();
	func2();
}

编译:

zkj@ubuntu:~/Desktop/test/t30$ gcc -g main.c t1.c t2.c -o test

执行结果如下:

zkj@ubuntu:~/Desktop/test/t30$ valgrind --tool=memcheck --leak-check=full --show-reachable=yes --trace-children=yes ./test 
==63012== Memcheck, a memory error detector
==63012== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==63012== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==63012== Command: ./test
==63012== 
==63012== 
==63012== HEAP SUMMARY:
==63012==     in use at exit: 4,040 bytes in 4 blocks
==63012==   total heap usage: 4 allocs, 0 frees, 4,040 bytes allocated
==63012== 
==63012== 200 bytes in 1 blocks are definitely lost in loss record 1 of 4
==63012==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==63012==    by 0x40053E: main (main.c:10)

==63012== 
==63012== 512 bytes in 1 blocks are definitely lost in loss record 2 of 4
==63012==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==63012==    by 0x400561: func1 (t1.c:9)
==63012==    by 0x400547: main (main.c:11)

==63012== 
==63012== 1,280 bytes in 1 blocks are definitely lost in loss record 3 of 4
==63012==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==63012==    by 0x40057A: func3 (t2.c:9)
==63012==    by 0x40059C: func2 (t2.c:16)
==63012==    by 0x40054C: main (main.c:12)

==63012== 
==63012== 2,048 bytes in 1 blocks are definitely lost in loss record 4 of 4
==63012==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==63012==    by 0x400593: func2 (t2.c:15)
==63012==    by 0x40054C: main (main.c:12)

==63012== 
==63012== LEAK SUMMARY:
==63012==    definitely lost: 4,040 bytes in 4 blocks
==63012==    indirectly lost: 0 bytes in 0 blocks
==63012==      possibly lost: 0 bytes in 0 blocks
==63012==    still reachable: 0 bytes in 0 blocks
==63012==         suppressed: 0 bytes in 0 blocks

==63012== 
==63012== For counts of detected and suppressed errors, rerun with: -v
==63012== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)
zkj@ubuntu:~/Desktop/test/t30$ 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寒江独钓2009

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值