Ubuntu C语言检测内存泄漏
来源:互联网 发布:筑巢软件怎么样 编辑:程序博客网 时间:2024/06/05 14:16
检测内存泄漏需利用工具Valgrind,
源码下载地址为:http://www.valgrind.org/downloads/current.html#current
其.deb相应版本的下载地址:http://pkgs.org/download/valgrind
测试的例子:
#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)
- Ubuntu C语言检测内存泄漏
- C语言内存泄漏检测
- C语言内存泄漏检测方法
- C语言检测内存泄漏方法valgrind
- C语言中内存泄漏的检测方法
- C代码检测内存泄漏
- c/c++内存泄漏检测
- C/C++检测内存泄漏
- C/C++内存泄漏检测
- C/C++内存泄漏检测
- 【C/C++】C/C++内存泄漏检测
- 在Go语言里检测内存泄漏
- C语言内存检测
- C/C++程序内存泄漏检测
- C/C++程序内存泄漏检测
- (转贴)C/C++程序内存泄漏检测
- C/C++内存泄漏及检测
- C/C++检测内存泄漏的方法
- matlab text 显示图像中或者画图中点的坐标 imshow显示图像,要用axis on显示坐标
- Android 带checkbox的listView 实现多选,全选,反选
- Struts 2的基石——拦截器(Interceptor)
- 提高web性能之--减少http请求
- 字符串处理函数
- Ubuntu C语言检测内存泄漏
- 三层结构功能解析
- 硬币问题
- 从键盘输入一个整数(1~20) 则以该数字为矩阵的大小,把1,2,3…n*n 的数字按照顺时针螺旋的形式填入其中。
- java中使用配置文件做数据库连接
- Mysql JDBC Url参数说明
- PLSQL开发笔记和小结
- 关于FLASH中的LOADER问题
- java List remove 时的小问题