一、valgrind的安装(Ubuntu)
sudo apt install valgrind //安装
valgrind --version //安装完检查版本信息确认
二、检测内存泄漏
例子:mem.c
#include <stdlib.h>
#include <stdio.h>
int main()
{
char *ptr;
ptr = (char *)malloc(10);
return 0;
}
执行
gcc -o mem mem.c -g
valgrind --tool=memcheck ./mem
打印信息:
==47084== Memcheck, a memory error detector
==47084== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==47084== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==47084== Command: ./mem
==47084==
==47084==
==47084== HEAP SUMMARY:
==47084== in use at exit: 10 bytes in 1 blocks //指示在程序退出时,还有多少内存没有释放
==47084== total heap usage: 1 allocs, 0 frees, 10 bytes allocated // 指示该执行过程malloc和free调用的次数
==47084==
==47084== LEAK SUMMARY:
==47084== definitely lost: 10 bytes in 1 blocks
==47084== indirectly lost: 0 bytes in 0 blocks
==47084== possibly lost: 0 bytes in 0 blocks
==47084== still reachable: 0 bytes in 0 blocks
==47084== suppressed: 0 bytes in 0 blocks
==47084== Rerun with --leak-check=full to see details of leaked memory
==47084==
==47084== For counts of detected and suppressed errors, rerun with: -v
==47084== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
查看具体的泄漏位置,执行
valgrind --leak-check=full ./mem
打印信息:
==47152== Memcheck, a memory error detector
==47152== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==47152== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==47152== Command: ./mem
==47152==
==47152==
==47152== HEAP SUMMARY:
==47152== in use at exit: 10 bytes in 1 blocks
==47152== total heap usage: 1 allocs, 0 frees, 10 bytes allocated
==47152==
==47152== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1
==47152== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) //malloc函数
==47152== by 0x10865B: main (mem.c:10) //第10行
==47152==
==47152== LEAK SUMMARY:
==47152== definitely lost: 10 bytes in 1 blocks
==47152== indirectly lost: 0 bytes in 0 blocks
==47152== possibly lost: 0 bytes in 0 blocks
==47152== still reachable: 0 bytes in 0 blocks
==47152== suppressed: 0 bytes in 0 blocks
==47152==
==47152== For counts of detected and suppressed errors, rerun with: -v
==47152== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
三、检测其他
例子:mem.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
char *ptr = malloc(10);
ptr[12] = 'a'; // 数组越界
free(ptr);
free(ptr);// 重复释放
char *p1;
*p1 = '1'; // 非法指针
return 0;
}
执行
valgrind --tool=memcheck ./mem
打印信息:
==47294== Memcheck, a memory error detector
==47294== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==47294== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==47294== Command: ./mem
//提示堆数组越界
==47294== Invalid write of size 1
==47294== at 0x108787: main (mem.c:8)
==47294== Address 0x522d04c is 2 bytes after a block of size 10 alloc'd
==47294== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==47294== by 0x10877A: main (mem.c:7)
//提示重复释放
==47294== Invalid free() / delete / delete[] / realloc()
==47294== at 0x4C30D3B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==47294== by 0x1087C1: main (mem.c:13)
==47294== Address 0x522d040 is 0 bytes inside a block of size 10 free'd
==47294== at 0x4C30D3B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==47294== by 0x1087B5: main (mem.c:12)
==47294== Block was alloc'd at
==47294== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==47294== by 0x10877A: main (mem.c:7)
//提示非法指针
==47294== Use of uninitialised value of size 8
==47294== at 0x1087C6: main (mem.c:15)
==47294==
==47294==
==47294== Process terminating with default action of signal 11 (SIGSEGV)
==47294== Bad permissions for mapped region at address 0x1087F0
==47294== at 0x1087C6: main (mem.c:15)
==47294==
==47294== HEAP SUMMARY:
==47294== in use at exit: 0 bytes in 0 blocks
==47294== total heap usage: 1 allocs, 2 frees, 10 bytes allocated
==47294==
==47294== All heap blocks were freed -- no leaks are possible
==47294==
==47294== For counts of detected and suppressed errors, rerun with: -v
==47294== Use --track-origins=yes to see where uninitialised values come from
==47294== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)
段错误 (核心已转储)
更多错误提示及例子可参考这篇博客:https://blog.youkuaiyun.com/u010168781/article/details/83749609