linux 内存检测工具之memleak

概述

MemLeak 是一个C语言内存泄漏检测工具。 原理是利用 C 语言的宏调用来替代原有的函数调用, memleak 维护了一个链表,在这个链表中保存着程序中对内存函数

调用的记录,这些函数包括:malloc、calloc、realloc、free。


接口声明(对外)

memleak.h代码如下:
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /*************************************************************************** 
  2.    memleak.h: Redirects calls to the functions defined in memleak.c 
  3.    Copyright (c) 2005, 2008 Stanislav Maslovski. 
  4.  
  5.    This program is free software; you can redistribute it and/or modify 
  6.    it under the terms of the GNU General Public License as published by 
  7.    the Free Software Foundation; either version 2 of the License, or 
  8.    (at your option) any later version. 
  9.  
  10.    This program is distributed in the hope that it will be useful, 
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of 
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  13.    GNU General Public License for more details. 
  14.  
  15.    You should have received a copy of the GNU General Public License 
  16.    along with this program; if not, write to the Free Software 
  17.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
  18.    ************************************************************************* 
  19.     
  20.    Include this file into the module you want to debug for memory leaks. 
  21.     
  22.    IMPORTANT: 
  23.    ========== 
  24.    If you do not want to catch ramdom segfaults make sure that this file gets 
  25.    included in _every_ module which uses malloc(), free() and friends. 
  26.    Only if you know that the blocks allocated in a particular module will never 
  27.    be freed or reallocated in any other module you may include this file only in 
  28.    that module. For additional information see memleak.c 
  29. */  
  30.   
  31. #ifndef H_MEMLEAK_H  
  32. #define H_MEMLEAK_H  
  33.   
  34. /* this makes sure that stdlib.h gets included before our macros */  
  35. #include <stdlib.h>  
  36.   
  37. extern char *dbg_file_name;  
  38. extern unsigned long dbg_line_number;  
  39.   
  40. #define CHK_FREED 1  
  41. #define CHK_ALLOC 2  
  42. #define CHK_ALL (CHK_FREED | CHK_ALLOC)  
  43.   
  44. extern void *dbg_malloc(size_t size);  
  45. extern void *dbg_realloc(void *ptr, size_t size);  
  46. extern void *dbg_calloc(size_t num, size_t size);  
  47. extern void dbg_free(void *ptr);  
  48.   
  49. extern void dbg_init(int history_length);  
  50. extern int dbg_check_addr(char *msg, void *ptr, int opt);  
  51. extern void dbg_mem_stat(void);  
  52. extern void dbg_zero_stat(void);  
  53. extern void dbg_abort(char *msg);  
  54. extern void dbg_heap_dump(char *keyword);  
  55. extern void dbg_history_dump(char *keyword);  
  56. extern void dbg_catch_sigsegv(void);  
  57.   
  58. #define FILE_LINE dbg_file_name = __FILE__, dbg_line_number = __LINE__  
  59. #define malloc(s) (FILE_LINE, dbg_malloc(s))  
  60. #define realloc(p, s) (FILE_LINE, dbg_realloc(p, s))  
  61. #define calloc(n, s) (FILE_LINE, dbg_calloc(n, s))  
  62. #define free(p) (FILE_LINE, dbg_free(p))  
  63.   
  64. #define dbg_init(n) (FILE_LINE, dbg_init(n))  
  65. #define dbg_check_addr(m, p, o) (FILE_LINE, dbg_check_addr(m, p, o))  
  66. #define dbg_mem_stat() (FILE_LINE, dbg_mem_stat())  
  67. #define dbg_zero_stat() (FILE_LINE, dbg_zero_stat())  
  68. #define dbg_abort(m) (FILE_LINE, dbg_abort(m))  
  69. #define dbg_heap_dump(k) (FILE_LINE, dbg_heap_dump(k))  
  70. #define dbg_history_dump(k) (FILE_LINE, dbg_history_dump(k))  
  71. #define dbg_catch_sigsegv() (FILE_LINE, dbg_catch_sigsegv())  
  72.   
  73. #ifdef WITH_DBG_STRDUP  
  74. /* this makes sure that string.h gets included before our macros */  
  75. #include <string.h>  
  76.   
  77. extern char *dbg_strdup(const char *s);  
  78. extern char *dbg_strndup(const char *s, size_t n);  
  79.   
  80. #define strdup(s) (FILE_LINE, dbg_strdup(s))  
  81. #define strndup(s, n) (FILE_LINE, dbg_strndup(s, n))  
  82.   
  83. #endif  
  84.   
  85. #endif  

  通过定义的struct head 链表接口体来记录所有内存申请来源(文件,行数,大小),dbg_heap_dump()有可以打印出所有内存的分配情况.

memleak源码

sourceforge下载源码地址: http://sourceforge.net/projects/memleak/

测试代码

example.c代码如下:
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <stdlib.h>  
  2. #include <stdio.h>  
  3. #include <unistd.h>  
  4. #include "memleak.h"  
  5.   
  6. int main()  
  7. {  
  8.   void *a,*b,*c;  
  9.   char *s;  
  10.   dbg_init(10);  
  11.   dbg_catch_sigsegv();  
  12.   a = malloc(1000);  
  13.   b = malloc(30);  
  14.   a = realloc(a, 150);  
  15.   c = calloc(90, 3);  
  16.   b = realloc(b, 0);  
  17.   
  18.   malloc(0);  
  19.   calloc(0, 10);  
  20.   realloc(0, 10);  
  21.   
  22.   realloc(a, 0);  
  23.   
  24.   free(0);  
  25.   
  26.   s = strdup("A string.");  
  27.   s = strndup(s, 5);  
  28.   
  29.   puts(s);  
  30.   
  31.   dbg_mem_stat();  
  32.   dbg_heap_dump("");  
  33.   return 0;  
  34. }  

编译运行

   直接make生成example,运行即可查看所有内存的分配使用情况,也可修example.c文件测试代码,对自己的程序进行验证内存问题.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值