使用valgrind检查内存使用问题

介绍

Valgrind是用于内存检泄漏检测、内存调试以及性能分析的工具集。这里主要介绍其用与内存相关的工具Memcheck,它可以发现大部分的内存问题,例如内存泄漏,使用未初始化内存、内存越界等问题。

安装

我用的时Ubuntu,首先安装Valgrind工具:

 sudo apt-get install valgrind

安装完成后,可以查看是否安装成功:

 valgrind --help

使用

Valgrind使用比较简单:

valgrind --tool=memcheck Path ./excuteFile

注意,在编译为可执行文件时,最好加上-g选项,这样可以看到程序内存问题出现的语句;加上-O0选项,不让编译器给程序做优化。

使用未初始化内存

 #include <iostream>
int main()
{
    int* p;
    *p=10;
    return 0;
}

编译

g++ test1.cpp -g -O0  -o test1

使用Valgrind查看

$ valgrind --tool=memcheck ./test1 
==3978== Memcheck, a memory error detector
==3978== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==3978== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==3978== Command: ./test1
==3978== 
==3978== Use of uninitialised value of size 8
==3978==    at 0x4006D5: main (test1.cpp:5)
==3978== 
==3978== Invalid write of size 4
==3978==    at 0x4006D5: main (test1.cpp:5)
==3978==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==3978== 
==3978== 
==3978== Process terminating with default action of signal 11 (SIGSEGV)
==3978==  Access not within mapped region at address 0x0
==3978==    at 0x4006D5: main (test1.cpp:5)
==3978==  If you believe this happened as a result of a stack
==3978==  overflow in your program's main thread (unlikely but
==3978==  possible), you can try to increase the size of the
==3978==  main thread stack using the --main-stacksize= flag.
==3978==  The main thread stack size used in this run was 8388608.
==3978== 
==3978== HEAP SUMMARY:
==3978==     in use at exit: 0 bytes in 0 blocks
==3978==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==3978== 
==3978== All heap blocks were freed -- no leaks are possible
==3978== 
==3978== For counts of detected and suppressed errors, rerun with: -v
==3978== Use --track-origins=yes to see where uninitialised values come from
==3978== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

1、3978是进程ID,最前面那个行是Valgrind的版本信息。
2、后面指出使用未初始化的内存,大小为8,错误发生的位置。
3、最后HEAP SUMMARY说明在HEAP上内存使用情况。
整个输出大概就是以上几个部分。

越界使用内存

#include <iostream>
int main()
{
    int *p=new int[10];
    *(p+10)=1;
    delete[] p;
    return 0;
}

输出:

==4297== Memcheck, a memory error detector
==4297== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==4297== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==4297== Command: ./test2
==4297== 
==4297== Invalid write of size 4
==4297==    at 0x40077B: main (test2.cpp:5)
==4297==  Address 0x5a1d068 is 0 bytes after a block of size 40 alloc'd
==4297==    at 0x4C2B800: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4297==    by 0x40076E: main (test2.cpp:4)
==4297== 
==4297== 
==4297== HEAP SUMMARY:
==4297==     in use at exit: 0 bytes in 0 blocks
==4297==   total heap usage: 1 allocs, 1 frees, 40 bytes allocated
==4297== 
==4297== All heap blocks were freed -- no leaks are possible
==4297== 
==4297== For counts of detected and suppressed errors, rerun with: -v
==4297== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

内存泄漏

#include <iostream>
int main()
{
    int *p=new int[10];
    *(p+1)=1;
    return 0;
}

输出:

==4318== Memcheck, a memory error detector
==4318== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==4318== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==4318== Command: ./test3
==4318== 
==4318== 
==4318== HEAP SUMMARY:
==4318==     in use at exit: 40 bytes in 1 blocks
==4318==   total heap usage: 1 allocs, 0 frees, 40 bytes allocated
==4318== 
==4318== LEAK SUMMARY:
==4318==    definitely lost: 40 bytes in 1 blocks
==4318==    indirectly lost: 0 bytes in 0 blocks
==4318==      possibly lost: 0 bytes in 0 blocks
==4318==    still reachable: 0 bytes in 0 blocks
==4318==         suppressed: 0 bytes in 0 blocks
==4318== Rerun with --leak-check=full to see details of leaked memory
==4318== 
==4318== For counts of detected and suppressed errors, rerun with: -v
==4318== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

两次释放内存

#include <iostream>
int main()
{
    int *p=new int[10];
    *(p+1)=1;
    delete[] p;
    delete[] p;
    return 0;
}

输出:

==4330== Memcheck, a memory error detector
==4330== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==4330== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==4330== Command: ./test4
==4330== 
==4330== Invalid free() / delete / delete[] / realloc()
==4330==    at 0x4C2C83C: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4330==    by 0x4007A6: main (test4.cpp:7)
==4330==  Address 0x5a1d040 is 0 bytes inside a block of size 40 free'd
==4330==    at 0x4C2C83C: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4330==    by 0x400793: main (test4.cpp:6)
==4330== 
==4330== 
==4330== HEAP SUMMARY:
==4330==     in use at exit: 0 bytes in 0 blocks
==4330==   total heap usage: 1 allocs, 2 frees, 40 bytes allocated
==4330== 
==4330== All heap blocks were freed -- no leaks are possible
==4330== 
==4330== For counts of detected and suppressed errors, rerun with: -v
==4330== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

使用已经释放的内存

#include <iostream>
int main()
{
    int *p=new int[10];
    *(p+1)=1;
    delete[] p;
    *(p+2)=10;
    return 0;
}

输出:

==4341== Memcheck, a memory error detector
==4341== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==4341== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==4341== Command: ./test5
==4341== 
==4341== Invalid write of size 4
==4341==    at 0x40079C: main (test5.cpp:7)
==4341==  Address 0x5a1d048 is 8 bytes inside a block of size 40 free'd
==4341==    at 0x4C2C83C: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4341==    by 0x400793: main (test5.cpp:6)
==4341== 
==4341== 
==4341== HEAP SUMMARY:
==4341==     in use at exit: 0 bytes in 0 blocks
==4341==   total heap usage: 1 allocs, 1 frees, 40 bytes allocated
==4341== 
==4341== All heap blocks were freed -- no leaks are possible
==4341== 
==4341== For counts of detected and suppressed errors, rerun with: -v
==4341== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

不匹配使用new/delete或new[]/delete[]

#include <iostream>
int main()
{
    int *p=new int[10];
    *(p+1)=1;
    delete p;
    return 0;
}

输出:

==4375== Memcheck, a memory error detector
==4375== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==4375== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==4375== Command: ./test6
==4375== 
==4375== Mismatched free() / delete / delete []
==4375==    at 0x4C2C2BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4375==    by 0x40078C: main (test6.cpp:6)
==4375==  Address 0x5a1d040 is 0 bytes inside a block of size 40 alloc'd
==4375==    at 0x4C2B800: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4375==    by 0x40076E: main (test6.cpp:4)
==4375== 
==4375== 
==4375== HEAP SUMMARY:
==4375==     in use at exit: 0 bytes in 0 blocks
==4375==   total heap usage: 1 allocs, 1 frees, 40 bytes allocated
==4375== 
==4375== All heap blocks were freed -- no leaks are possible
==4375== 
==4375== For counts of detected and suppressed errors, rerun with: -v
==4375== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值