设为首页 加入收藏

TOP

Linux C程序内存泄露检测
2014-11-24 00:58:15 来源: 作者: 【 】 浏览:2
Tags:Linux 程序 内存 泄露 检测

对于程序员来说,最痛苦的就是内存的申请与释放。内存泄露也是程序中经常遇到的问题。为了更好的定位内存泄露问题,我们有必要熟悉一些内存泄露的检测工具。今天主要找到了以下四个内存检测工具,使用起来都比较方便。


安装valgrind,执行下列程序


#include


void func()
{
int *p = malloc(10*sizeof(int));
p[10] = 0;
}


int main()
{
func();
return 0;
}


编译:
gcc -g -o valgrindtst valgrindtst.c


执行内存检测:


valgrind --tool=memcheck -leak-check=full ./valgrindtst


内存检测结果:


==19896== Memcheck, a memory error detector
==19896== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==19896== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==19896== Command: ./valgrindtst
==19896==
==19896== Invalid write of size 4
==19896== at 0x80483DF: func (valgrindtst.c:6)
==19896== by 0x80483F1: main (valgrindtst.c:11)
==19896== Address 0x401a050 is 0 bytes after a block of size 40 alloc'd
==19896== at 0x40072D5: malloc (vg_replace_malloc.c:291)
==19896== by 0x80483D5: func (valgrindtst.c:5)
==19896== by 0x80483F1: main (valgrindtst.c:11)
==19896==
==19896==
==19896== HEAP SUMMARY:
==19896== in use at exit: 40 bytes in 1 blocks
==19896== total heap usage: 1 allocs, 0 frees, 40 bytes allocated
==19896==
==19896== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==19896== at 0x40072D5: malloc (vg_replace_malloc.c:291)
==19896== by 0x80483D5: func (valgrindtst.c:5)
==19896== by 0x80483F1: main (valgrindtst.c:11)
==19896==
==19896== LEAK SUMMARY:
==19896== definitely lost: 40 bytes in 1 blocks
==19896== indirectly lost: 0 bytes in 0 blocks
==19896== possibly lost: 0 bytes in 0 blocks
==19896== still reachable: 0 bytes in 0 blocks
==19896== suppressed: 0 bytes in 0 blocks
==19896==
==19896== For counts of detected and suppressed errors, rerun with: -v
==19896== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 12 from 8)


检测到内存泄露与数组越界的错误


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇二叉搜索树之Java实现 下一篇jQuery判断元素是否存在

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: