memwatch内存泄露检测工具

工具介绍

官网

http://www.linkdata.se/sourcecode/memwatch/

其功能如下官网介绍,挑选重点整理:

1、 号称功能: 内存泄露检测 (检测未释放内存, 即 动态内存开辟未释放的情况)

2、 检测 多次调用free, 和 free 错误地址

3、 检测内存访问的 上越界 和 下越界

4、 检测对野指针进行的写操作

其他内存检测工具有 mtrace valgrind 参考 http://www.cnblogs.com/honglihua8688/p/3727944.html

A memory leak detection tool. Basically, you add a header file to your souce code files, and compile with MEMWATCH defined or not. The header file MEMWATCH.H contains detailed instructions. This is a list of some of the features present in version 2.71:
- ANSI C
- Logging to file or user function using TRACE() macro
- Fault tolerant, can repair it’s own data structures
- Detects double-frees and erroneous free’s
- Detects unfreed memory
- Detects overflow and underflow to memory buffers
- Can set maximum allowed memory to allocate, to stress-test app
- ASSERT(expr) and VERIFY(expr) macros
- Can detect wild pointer writes
- Support for OS specific address validation to avoid GP’s (segmentation faults)
- Collects allocation statistics on application, module or line level
- Rudimentary support for threads (see FAQ for details)
- Rudimentary support for C++ (disabled by default, use with care!)

工具使用

下载工具后, 解压查看其中主要文件为 

    memwatch.h
    memwatch.c
    test.c

    Makefile

http://www.linkdata.se/downloads/sourcecode/memwatch/memwatch-2.71.tar.gz

直接执行make命令, 则会报错, 需要注释掉 最后一行, 开发者想让使用者通过 test.c 熟悉注释, 通过注释了解使用方法

/* Comment out the following line to compile.
#error "Hey! Don't just compile this program, read the comments first!"
*/

make && ./test 则发现 此文件夹下多了一个log文件

    memwatch.log

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

可见使用memwatch很方便, 只需要将 memwatch.c 和memwatch.h移植到你的待检测程序代码中

通过makefile将 memwatch编译进去,需要关注的是, 编译时加上-DMEMWATCH -DMW_STDIO

$(CC) -DMEMWATCH -DMW_STDIO test.c memwatch.c -o test

 

test.c demo

test.c 代码如下

/*
**  NOTE: Running this program in a Win32 or Unix environment
**  will probably result in a segmentation fault or protection
**  error. These errors may be caused by MEMWATCH when it is
**  looking at memory to see if it owns it, or may be caused by
**  the test program writing to memory it does not own.
**
**  MEMWATCH has two functions called 'mwIsReadAddr()' and
**  'mwIsSafeAddr()', which are system-specific.
**  If they are implemented for your system, and works
**  correctly, MEMWATCH will identify garbage pointers and
**  avoid causing segmentation faults, GP's etc.
**
**  If they are NOT implemented, count on getting the core
**  dumped when running this test program! As of this writing,
**  the safe-address checking has been implemented for Win32
**  and ANSI-C compliant systems. The ANSI-C checking traps
**  SIGSEGV and uses setjmp/longjmp to resume processing.
**
**  Note for Win95 users: The Win32 IsBadReadPtr() and its
**  similar functions can return incorrect values. This has
**  not happened under WinNT, though, just Win95.
**
**  991009 Johan Lindh
**
*/#include <stdio.h>
#include <signal.h>
#include "memwatch.h"#ifndef SIGSEGV
#error "SIGNAL.H does not define SIGSEGV; running this program WILL cause a core dump/crash!"
#endif#ifndef MEMWATCH
#error "You really, really don't want to run this without memwatch. Trust me."
#endif#if !defined(MW_STDIO) && !defined(MEMWATCH_STDIO)
#error "Define MW_STDIO and try again, please."
#endifint main()
{char *p;/* Collect stats on a line number basis */mwStatistics( 2 );/* Slows things down, but OK for this test prg *//* mwAutoCheck( 1 ); */TRACE("Hello world!\n");p = malloc(210);free(p);p = malloc(20);p = malloc(200);    /* causes unfreed error */p[-1] = 0;          /* causes underflow error */free(p);p = malloc(100);p[ -(int)(sizeof(long)*8) ] = -1; /* try to damage MW's heap chain */free( p ); /* should cause relink */mwSetAriFunc( mwAriHandler );ASSERT(1==2);mwLimit(1000000);mwNoMansLand( MW_NML_ALL );/* These may cause a general protection fault (segmentation fault) *//* They're here to help test the no-mans-land protection */if( mwIsSafeAddr(p+50000,1) ) {TRACE("Killing byte at %p\n", p+50000);*(p+50000) = 0;}if( mwIsSafeAddr(p+30000,1) ) {TRACE("Killing byte at %p\n", p+30000);*(p+30000) = 0;}if( mwIsSafeAddr(p+1000,1) ) {TRACE("Killing byte at %p\n", p+1000);*(p+1000) = 0;}if( mwIsSafeAddr(p-100,1) ) {TRACE("Killing byte at %p\n", p-100);*(p-100) = 0;}/* This may cause a GP fault as well, since MW data buffers  *//* have been damaged in the above killing spree */CHECK();p = malloc(12000);p[-5] = 1;p[-10] = 2;p[-15] = 3;p[-20] = 4;/* This may cause a GP fault since MW's buffer list may have *//* been damaged by above killing, and it will try to repair it. */free(p);p = realloc(p,10);	/* causes realloc: free'd from error *//* May cause GP since MW will inspect the memory to see if it owns it. */free( (void*)main );return 0;
}/* Comment out the following line to compile. 
#error "Hey! Don't just compile this program, read the comments first!"
*/

 

memwatch.log内容, 如下粗体字标注, 有泄露检测结果 和 越界检测结果

============= MEMWATCH 2.71 Copyright (C) 1992-1999 Johan Lindh =============Started at Wed Jul 15 22:04:06 2015Modes: __STDC__ 64-bit mwDWORD==(unsigned long)
mwROUNDALLOC==8 sizeof(mwData)==32 mwDataSize==32statistics: now collecting on a line basis
Hello world!
underflow: <5> test.c(62), 200 bytes alloc'd at <4> test.c(60)
assert trap: <8> test.c(69), 1==2
assert trap: <8> IGNORED - execution continues
limit: old limit = none, new limit = 1000000 bytes
grabbed: all allowed memory to no-mans-land (976 kb)
Killing byte at 0x84496f0
Killing byte at 0x84448d0
Killing byte at 0x843d788
Killing byte at 0x843d33c
check: <8> test.c(95), checking chain alloc nomansland 
check: <8> test.c(95), complete; no errors
internal: <10> test.c(105), checksum for MW-0x85331f8 is incorrect
underflow: <10> test.c(105), 0 bytes alloc'd at <9> test.c(865)
overflow: <10> test.c(105), 0 bytes alloc'd at <9> test.c(865)
internal: <10> test.c(107), no-mans-land MW-0x85331f8 is corrupted
realloc: <10> test.c(107), 0x8533220 was freed from test.c(105)
WILD free: <11> test.c(110), unknown pointer 0x8048a3bStopped at Wed Jul 15 22:04:21 2015wild pointer: <11> no-mans-land memory hit at 0x84496f0
wild pointer: <11> no-mans-land memory hit at 0x84448d0
wild pointer: <11> no-mans-land memory hit at 0x843d788
dropped: all no-mans-land memory (976 kb)
unfreed: <3> test.c(59), 20 bytes at 0x843d1d0  	{FE FE FE FE FE FE FE FE FE FE FE FE FE FE FE FE ................}Memory usage statistics (global):N)umber of allocations made: 5L)argest memory usage      : 12020T)otal of all alloc() calls: 12530U)nfreed bytes totals      : 12020Memory usage statistics (detailed):Module/Line                                Number   Largest  Total    Unfreed %s0        0        0        -100    64                                        0        0        0        -100    test.c                                     5        12120    12530    12120   865                                       0        0        0        0       97                                        1        12000    12000    12000   64                                        1        100      100      100     60                                        1        200      200      0       59                                        1        20       20       20      57                                        1        210      210      0       

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/288290.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Windows命令大全

C:\Users\Administrator>help 有关某个命令的详细信息,请键入 HELP 命令名 ASSOC 显示或修改文件扩展名关联。 ATTRIB 显示或更改文件属性。 BREAK 设置或清除扩展式 CTRL+C 检查。 BCDEDIT 设置启动数据库中的属性以控制启动加载。 CACL…

command not found Operation not permitted

mysql -uroot -p 报错误:command not found因为苹果在OS X 10.11中引入的SIP特性使得即使加了sudo&#xff08;也就是具有root权限&#xff09;也无法修改系统级的目录&#xff0c;其中就包括了/usr/bin。要解决这个问题有两种做法&#xff1a;比较不安全的就是关闭SIP&#x…

Main 和 静态构造函数 到底谁先执行?

最近被问到一个很有意思的问题&#xff0c;到底是 Main函数 先执行还是 静态构造函数 先执行&#xff1f;参考如下代码&#xff1a;class Program{static Program(){Console.WriteLine("我是 静态构造 函数&#xff01;");}static void Main(string[] args){Console.…

c 正则提取html,c – 正则表达式以获取HTML表格内容

确实没有可能的正则表达式解决方案适用于任意数量的表数据,并将每个单元格放入单独的后向引用中.这是因为通过反向引用,您需要为要创建的每个backref创建一个独特的开放式窗口,并且您不知道自己有多少个单元格.使用一种或另一种循环来提取数据没有任何问题.例如,在最后一个,在P…

(五)python3 只需3小时带你轻松入门—— 逻辑运算符

如果if判断中存在多个表达式判断&#xff0c;需要使用逻辑运算符。 例如有一个变量a&#xff0c;需要判断是否在1到5之间&#xff0c;那么则需要判断a是否大于1且a小于5。这个时候需要使用and逻辑运算符进行判断。 and python中使用and判断左右两边表达式是同时正确&#xff0c…

[linux]unixODBC的安装配置说明

什么是unixODBC&#xff1a; ODBC(Open Database Connect)是由Microsoft 公司于1991 年提出的一个开放的&#xff0c;用于访问数据库的统一接口规范。 unixODBC的是为非Window平台的系统实现ODBC规范接口的中间件。 unixODBC的安装 有GUI的用户可以使用ODBCConfig tool 安装。教…

linux之systemctl设置自定义服务

1 问题 在linux系统上设置自定义服务,或者让我的服务开机就运行起来,比如我们就以tomcat服务为例子 /usr/local/tomcat/bin/startup.sh 启动tomcat的脚本/usr/local/tomcat/bin/shutdown.sh 关闭tomcat的脚本/usr/local/tomcat/bin/restart.sh 重启tomcat的脚本 2 解决方法 …

ASP.NET MVC入门(一)---MVC的Hello World

简单的MVC Hello world,着重处理Controller。 Step1 创建一个Asp.Net MVC 5项目打开Visual studio 2013 点“文件”->新建->项目。

for循环批量写文件 shell_shell脚本:for循环批量重命名带空格文件名的文件

今天在学习shell脚本攻略第二章2.13 批量重命名和移动时,由于我的文件命令有的文件名字中出现空格,出现了许多问题,因此将解决问题的过程记录了下来,希望能够和大家交流学习,共同进步,谢谢!问题代码问题代码最终成功代码最终代码步骤一思路: 由于在shell中运行此脚本时,发现将找…

HDU 4864 Task(贪心或高斯消元)

题意&#xff1a;n台机器工作时间为ai,等级bi,m个任务需要时间ai&#xff0c;等级bi,一台机器只能完成一个任务&#xff0c;完成一个任务收益为500*ai2*bi,求最大收益&#xff1b;(n,m>100000) 思路&#xff1a;先从大到小排时间&#xff0c;再从大到小排等级&#xff0c;借…

android模拟器无法拍照,为什么dafault相机无法在android模拟器中工作?

当我试图打开摄像头应用程序在模拟器defaultly给出&#xff0c;它意外停止和logcat的显示为什么dafault相机无法在android模拟器中工作&#xff1f;02-08 11:18:35.005: INFO/ActivityManager(83): Starting activity: Intent {act android.intent.action.MAIN cat[android.int…

nginx作为tcp代理 虚拟主机配置 模板

# tcp协议虚拟主机的配置文件模板 # cat /usr/local/nginx/conf/tcp/tcp.test.com.conf user nginx; worker_processes auto; worker_rlimit_nofile 102400;error_log /data0/log/tcp.test.com.error.log info;events {use epoll;worker_connections 25600; }http {includ…

(四)python3 只需3小时带你轻松入门—— 流程控制

缩进 python中使用缩进代表代码块&#xff1b;每一个块代表一个层次&#xff08;分支&#xff09;&#xff0c;每个单独的分支是独立的&#xff0c;但是从整体逻辑上又是相融的&#xff1b;就像一本书一样&#xff0c;每个知识点是独立的&#xff0c;但是每个知识点组成了这本书…

ASP.NET Core 正确获取查询字符串参数

前言有网友在交流群中询问&#xff0c;如何获取查询字符串参数&#xff1a;默认情况下&#xff0c;ASP.NET Core 的模型绑定以键值对的形式从 HTTP 请求中的以下列表中指示的顺序扫描源并获取数据&#xff1a;表单域请求正文路由数据查询字符串参数上传的文件因此&#xff0c;不…

poj 1318

http://poj.org/problem?id1318 这个题目还是比较水的&#xff0c;不过也可以提升你对字符串的熟悉度以及对一些排序函数和字符函数的使用。 大概的题意就是给你一个字典&#xff0c;这个字典有一些单词&#xff0c;先对这些单词排好序&#xff0c;然后再给你一串字母&#xf…

C语言之#和##和__VA_ARGS__和##__VA_ARGS__

1、问题 看到代码宏里面很多##__VA_ARGS__不知道什么意思 2、直接写测试代码 #include <stdio.h>#define my_printf(x) printf(#x" is %d\n", x) #define my_printf1(x) printf("value is %d\n", x##x) #define my_printf2(...) printf(__VA_ARGS_…

ASP.NET MVC入门(二)---MVC数据传递

先来看一个简单的从Model到View传递数据的例子。 1、Model 在Models文件夹下新建一个类: using System; using System.Collections.Generic; using System.Linq; using System.Web;namespace Demo02_MVC数据传递.Models {public class Man{public string name { get; set; }p…

(三)python3 只需3小时带你轻松入门—— 变量的简单运算

变量运算 在编程时&#xff0c;需要对数据进行计算&#xff0c;计算的形式不限于&#xff1a;字符串拼接、相加减、相乘除及普遍的数学运算、剔除或指定剔除、添加或指定添加等。 在python中&#xff1a; *表示乘法/表示除法表示加法-表示减法 a,b10,11 cog3 j2 print(ca)#加…

安川伺服总线通讯方式_MⅢ总线特点 安川伺服选型与应用案例

MⅢ总线特点 安川伺服选型与应用案例的详细描述&#xff1a;MⅢ总线特点 安川伺服选型与应用案例降低系统成本在MECHATROLINK-Ⅲ中&#xff0c;可连接62个站点&#xff0c;从而大幅度降低系统的配线费用与时间。与过程控制、工厂自动化领域中的连接端子和电缆配套使用&#xff…

使用XMLConfiguration解析xml,properties等相应信息

org.apache.commons.configuration.XMLConfiguration; Apache Common-Configuration工具可以从Properties文件&#xff0c;XML文件,JNDI,JDBC数据源&#xff0c;System Properties,Applet parameters,Servlet Parameters等读取相应信息 使用步骤 前提&#xff0c;引入commons-c…