在Linux下编写C程序,怎么检查程序是否有内存泄漏?
生活随笔
收集整理的這篇文章主要介紹了
在Linux下编写C程序,怎么检查程序是否有内存泄漏?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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) |
總結
以上是生活随笔為你收集整理的在Linux下编写C程序,怎么检查程序是否有内存泄漏?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 导入JasperReports坐标时无法
- 下一篇: Linux C 预处理详解