admin管理员组文章数量:1568306
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$
版权声明:本文标题:介绍一下内存泄漏工具valgrind的使用 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1725886477a1047063.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论