在VC中检测内存泄漏
声明:checkleaks.h和checkleaks.cpp这两个文件中的代码是在网上COPY的,但原来那个网站现在却找不到了
所以,这篇文章不算完全原创,呵呵。
就当作是一个存档吧
先上代码:
- //checkleaks.h
- #ifndef SET_DEBUG_NEW_H
- #define SET_DEBUG_NEW_H
- #ifdef _DEBUG
- #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
- #else
- #define DEBUG_CLIENTBLOCK
- #endif
- #define _CRTDBG_MAP_ALLOC
- #include <crtdbg.h>
- #ifdef _DEBUG
- #define new DEBUG_CLIENTBLOCK
- #endif
- #pragma once
- #if defined(WIN32)
- void setFilterDebugHook(void);
- #endif
- #endif
- //checkleaks.cpp
- #if defined(WIN32)
- #include <string.h>
- #include "crtdbg.h"
- #define FALSE 0
- #define TRUE 1
- _CRT_REPORT_HOOK prevHook;
- int reportingHook(int reportType, char* userMessage, int* retVal)
- {
- // This function is called several times for each memory leak.
- // Each time a part of the error message is supplied.
- // This holds number of subsequent detail messages after
- // a leak was reported
- const int numFollowupDebugMsgParts = 2;
- static bool ignoreMessage = false;
- static int debugMsgPartsCount = 0;
- // check if the memory leak reporting starts
- if ((strncmp(userMessage,"Detected memory leaks!/n", 10) == 0)
- || ignoreMessage)
- {
- // check if the memory leak reporting ends
- if (strncmp(userMessage,"Object dump complete./n", 10) == 0)
- {
- _CrtSetReportHook(prevHook);
- ignoreMessage = false;
- } else
- ignoreMessage = true;
- // something from our own code?
- if(strstr(userMessage, ".cpp") == NULL)
- {
- if(debugMsgPartsCount++ < numFollowupDebugMsgParts)
- // give it back to _CrtDbgReport() to be printed to the console
- return FALSE;
- else
- return TRUE; // ignore it
- } else
- {
- debugMsgPartsCount = 0;
- // give it back to _CrtDbgReport() to be printed to the console
- return FALSE;
- }
- } else
- // give it back to _CrtDbgReport() to be printed to the console
- return FALSE;
- };
- void setFilterDebugHook(void)
- {
- //change the report function to only report memory leaks from program code
- prevHook = _CrtSetReportHook(reportingHook);
- }
- #endif
- //main.cpp
- #include <windows.h>
- #include <stdio.h>
- #include "checkleaks.h"
- int main()
- {
- //一定要加上这句,而且要在DEBUG模式下才会有报告输出
- _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
- int *arr;
- arr = new int;
- return 0;
- }
程序输出:
- Detected memory leaks!
- Dumping objects ->
- e:\vs2008\try\ctemp\ctemp\main.cpp(9) : {62} client block at 0x00503DE8, subtype 0, 4 bytes long.
- Data: < > CD CD CD CD
- Object dump complete.
- The program '[3348] ctemp.exe: Native' has exited with code 0 (0x0).
http://blog.csdn.net/small_qch/article/details/6856445
在VC中检测内存泄漏的更多相关文章
- VC使用CRT调试功能来检测内存泄漏
信息来源:csdn C/C++ 编程语言的最强大功能之一便是其动态分配和释放内存,但是中国有句古话:“最大的长处也可能成为最大的弱点”,那么 C/C++ 应用程序正好印证了这句话.在 C/C+ ...
- 在VC++中启用内存泄露检测
检测内存泄漏的主要工具是调试器和 CRT 调试堆函数.若要启用调试堆函数,请在程序中包括以下语句: #define CRTDBG_MAP_ALLOC#include <stdlib.h># ...
- Vc 检测内存泄漏
启用内存泄漏检测 检测内存泄漏是 C/c + + 调试器和 C 运行时库 (CRT) 的主要工具调试堆函数. 若要启用调试堆的所有函数,在 c + + 程序中,按以下顺序包含以下语句: C++复制 # ...
- 如何在linux下检测内存泄漏
之前的文章应用 Valgrind 发现 Linux 程序的内存问题中介绍了利用Linux系统工具valgrind检测内存泄露的简单用法,本文实现了一个检测内存泄露的工具,包括了原理说明以及实现细节. ...
- 如何在linux下检测内存泄漏(转)
本文转自:http://www.ibm.com/developerworks/cn/linux/l-mleak/ 本文针对 linux 下的 C++ 程序的内存泄漏的检测方法及其实现进行探讨.其中包括 ...
- 使用_CRTDBG_LEAK_CHECK_DF检查VC程序的内存泄漏(转)
我们知道,MFC程序如果检测到存在内存泄漏,退出程序的时候会在调试窗口提醒内存泄漏.例如: class CMyApp : public CWinApp{public:BOOL InitApplicat ...
- Android性能优化之利用LeakCanary检测内存泄漏及解决办法
前言: 最近公司C轮融资成功了,移动团队准备扩大一下,需要招聘Android开发工程师,陆陆续续面试了几位Android应聘者,面试过程中聊到性能优化中如何避免内存泄漏问题时,很少有人全面的回答上来. ...
- 系统剖析Android中的内存泄漏
[转发]作为Android开发人员,我们或多或少都听说过内存泄漏.那么何为内存泄漏,Android中的内存泄漏又是什么样子的呢,本文将简单概括的进行一些总结. 关于内存泄露的定义,我可以理解成这样 没 ...
- 了解 JavaScript 应用程序中的内存泄漏
简介 当处理 JavaScript 这样的脚本语言时,很容易忘记每个对象.类.字符串.数字和方法都需要分配和保留内存.语言和运行时的垃圾回收器隐藏了内存分配和释放的具体细节. 许多功能无需考虑内存管理 ...
随机推荐
- ORACLE 索引概述
索引是数据库中一种可选的数据结构,她通常与表或簇相关.用户可以在表的一列或数列上建立索引,以提高在此表上执行 SQL 语句的性能.就像本文档的索引可以帮助读者快速定位所需信息一样,Oracle 的索引 ...
- Windows 如何在cmd命令行中查看、修改、删除与添加环境变量
转自:http://www.cnblogs.com/saptechnique/archive/2013/02/17/2914222.html 首先明确一点: 所有的在cmd命令行下对环境变量的修改只对 ...
- ubuntu下mysql的常用命令
首先安装mysql:sudo?apt-get?install?mysql-server?mysql-client? 1.终端启动MySQL:/etc/init.d/mysql start:(stop ...
- VB.NET版机房收费系统---七仙女之系统登录
VB.NET第一版机房收费系统,告一段落,验收的时候.问题也是大大的存在,没实用上设计模式,什么触发器.存储过程,都没实用上.看看其她小伙伴的,七层实现登录?那是什么东东,相比較我的三层而言,多了两倍 ...
- 【剑指offer】和为定值的连续正数序列
.可是他并不满足于此,他在想到底有多少种连续的正数序列的和为100(至少包含两个数).没多久,他就得到还有一组连续正数和为100的序列:18,19,20,21,22.如今把问题交给你,你能不能也非常快 ...
- StarUML中时序图添加小人
转载于 http://blog.csdn.net/longyuhome/article/details/9011629 在看时序图的例子的时候,发现有些的时序图上有小人的图标,可是一些UML工具却没有 ...
- mysql myisam 锁表问题<转>
转自http://yafei001.iteye.com/blog/1841258 锁是计算机协调多个进程或线程并发访问某一资源的机制 .在数据库中,除传统的计算资源(如CPU.RAM.I/O等)的争用 ...
- SpringMVC01
1.创建一个web项目 引入所需要的jar 2.在web.xml文件中配置 核心控制器 <?xml version="1.0" encoding="UTF-8&q ...
- poj 1988 Cube Stacking && codevs 1540 银河英雄传说(加权并茶几)
#include<iostream> #include<cstdio> #include<cstring> #define maxn 30010 using nam ...
- SVN版本日志对话框命令使用指南
日志使用指南 http://tortoisesvn.net/docs/nightly/TortoiseSVN_zh_CN/tsvn-dug-showlog.html