在Linux下编写C程序,怎么检查程序是否有内存泄漏?

104 篇文章 0 订阅

From: http://bbs.chinaunix.net/thread-908769-1-1.html

如题。

-------------------------------

找到了!  http://www.valgrind.org/

同时附一个转自 http://blog.chinaunix.net/u/18381/showart.php?id=162015 的示例:

#include <stdlib.h>

void f(void)
{
   int* x = malloc(10 * sizeof(int));
   x[10] = 0;        // problem 1: heap block overrun
}                    // problem 2: memory leak -- x not freed

int main(void)
{
     f();
     return 0;
}

编译代码:  
gcc -Wall example.c -g -o example 

注意:gcc 的-g 选项让Valgrind调试输出时指出相应信息的代码所在的行号。

运行valgrind: 
valgrind --tool=memcheck --leak-check=yes ./example

valgrind的执行结果: 
==6742== Memcheck, a memory error detector for x86-linux.
==6742== Copyright (C) 2002-2004, and GNU GPL'd, by Julian Seward et al.
==6742== Using valgrind-2.2.0, a program supervision framework for x86-linux.
==6742== Copyright (C) 2000-2004, and GNU GPL'd, by Julian Seward et al.
==6742== For more details, rerun with: -v
==6742==
==6742== Invalid write of size 4
==6742==    at 0x8048384: f (example.c:6)
==6742==    by 0x80483AC: main (example.c:12)
==6742==  Address 0x1B908050 is 0 bytes after a block of size 40 alloc'd
==6742==    at 0x1B904984: malloc (vg_replace_malloc.c:131)
==6742==    by 0x8048377: f (example.c:5)
==6742==    by 0x80483AC: main (example.c:12)
==6742==
==6742== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 12 from 1)
==6742== malloc/free: in use at exit: 40 bytes in 1 blocks.
==6742== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
==6742== For counts of detected errors, rerun with: -v
==6742== searching for pointers to 1 not-freed blocks.
==6742== checked 1360800 bytes.
==6742==
==6742==
==6742== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==6742==    at 0x1B904984: malloc (vg_replace_malloc.c:131)
==6742==    by 0x8048377: f (example.c:5)
==6742==    by 0x80483AC: main (example.c:12)
==6742==
==6742== LEAK SUMMARY:
==6742==    definitely lost: 40 bytes in 1 blocks.
==6742==    possibly lost:   0 bytes in 0 blocks.
==6742==    still reachable: 0 bytes in 0 blocks.
==6742==         suppressed: 0 bytes in 0 blocks.
==6742== Reachable blocks (those to which a pointer was found) are not shown.
==6742== To see them, rerun with: --show-reachable=yes

上面的C程序存在两个错误:1. 数组下标越界;2. 分配的内存没有释放,存在内存泄露的问题。
对于错误1,看Valgrind的调试信息片断
==6742== Invalid write of size 4
==6742==    at 0x8048384: f (example.c:6)
==6742==    by 0x80483AC: main (example.c:12)
==6742==  Address 0x1B908050 is 0 bytes after a block of size 40 alloc'd
==6742==    at 0x1B904984: malloc (vg_replace_malloc.c:131)
==6742==    by 0x8048377: f (example.c:5)

对于错误2,看这个
==6742== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
......
==6742== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==6742==    at 0x1B904984: malloc (vg_replace_malloc.c:131)
==6742==    by 0x8048377: f (example.c:5)
==6742==    by 0x80483AC: main (example.c:12)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当涉及到内存泄漏时,我们需要使用一些工具来检测和解决这个问题。其一个常用的工具是 Valgrind,它可以检测内存泄漏和其它内存错误。以下是一个简单的 C 语言程序,使用 Valgrind 来检测内存泄漏: ``` #include <stdlib.h> int main() { int *ptr = malloc(sizeof(int)); *ptr = 42; free(ptr); return 0; } ``` 在终端运行以下命令: ``` valgrind --leak-check=full ./a.out ``` Valgrind 将会输出内存泄漏的详细信息,如果没有内存泄漏,它将会输出类似于以下的信息: ``` ==12345== HEAP SUMMARY: ==12345== in use at exit: 0 bytes in 0 blocks ==12345== total heap usage: 1 allocs, 1 frees, 4 bytes allocated ==12345== ==12345== All heap blocks were freed -- no leaks are possible ==12345== ==12345== For counts of detected and suppressed errors, rerun with: -v ==12345== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) ``` 如果有内存泄漏,它将会输出类似于以下的信息: ``` ==12345== HEAP SUMMARY: ==12345== in use at exit: 4 bytes in 1 blocks ==12345== total heap usage: 2 allocs, 1 frees, 8 bytes allocated ==12345== ==12345== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==12345== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==12345== by 0x4005B7: main (in /path/to/program) ==12345== ==12345== LEAK SUMMARY: ==12345== definitely lost: 4 bytes in 1 blocks ==12345== indirectly lost: 0 bytes in 0 blocks ==12345== possibly lost: 0 bytes in 0 blocks ==12345== still reachable: 0 bytes in 0 blocks ==12345== suppressed: 0 bytes in 0 blocks ==12345== ==12345== For counts of detected and suppressed errors, rerun with: -v ==12345== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) ``` 这个程序没有内存泄漏,所以 Valgrind 输出了 "no leaks are possible" 的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值