一、非MFC程序可以用以下方法检测内存泄露:

1. 程序开始包含如下定义:

#ifdef _DEBUG

#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)

#else

#define DEBUG_CLIENTBLOCK

#endif   // _DEBUG

#define _CRTDBG_MAP_ALLOC

#include

#include

#ifdef _DEBUG

#define new   DEBUG_CLIENTBLOCK

#endif // _DEBUG

2.程序中添加下面的函数:

_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF);

Debug版本程序运行结束后如有内存泄漏,输出窗口中会显示类似信息:

Detected memory leaks!

Dumping objects -> g:\programs\test\test.cpp(16) :

{51} client block at 0x00385C58, subtype 0, 4 bytes long.

Data: < > CD CD CD CD Object dump complete.

二、MFC程序内存泄漏检测方法:

1. 在 CMyApp 中添加如下三个 CMemoryState 类的成员变量:

#ifdef _DEBUG

protected: CMemoryState m_msOld, m_msNew, m_msDiff;

#endif // _DEBUG

2. 在 CMyApp::InitInstance() 中添加如下代码:

#ifdef _DEBUG

m_msOld.Checkpoint();

#endif // _DEBUG

3.在 CMyApp::ExitInstance() 中添加如下代码:

#ifdef _DEBUG

m_msNew.Checkpoint();

if (m_msDiff.Difference(m_msOld, m_msNew))

{

afxDump<<"\nMemory Leaked :\n";

m_msDiff.DumpStatistics();

afxDump<<"Dump Complete !\n\n";

}

#endif // _DEBUG

Debug版本程序运行结束后如有内存泄漏,输出窗口中会显示类似信息:

Memory Leaked :

0 bytes in 0 Free Blocks. 8 bytes in 1 Normal Blocks.

0 bytes in 0 CRT Blocks. 0 bytes in 0 Ignore Blocks.

0 bytes in 0 Client Blocks. Largest number used: 8825 bytes.

Total allocations: 47506 bytes.

Dump Complete !

Detected memory leaks!

Dumping objects -> g:\programs\chat\chatdlg.cpp(120) :

{118} normal block at 0x00D98150, 8 bytes long.

Data: < > A8 7F D9 00 01 00 00 00 Object dump complete.

分类: VC++ 2011-06-04 16:28 810人阅读 评论(1) 收藏 举报

开发工具:VS2005。(在VC++6.0中好像也可以这么用)

目的:检测C++代码中有无内存泄漏(即动态分配了内存,而没有正确释放)。

操作步骤:

一、把以下debug_new.h和debug_new.cpp文件加入项目中。

debug_new.h

  1. #ifndef _DEBUG_NEW_H_
  2. #define _DEBUG_NEW_H_
  3. #ifdef _DEBUG
  4. #undef new
  5. extern void _RegDebugNew( void );
  6. extern void* __cdecl operator new( size_t, const char*, int );
  7. extern void __cdecl operator delete( void*, const char*, int);
  8. #define new new(__FILE__, __LINE__)
  9. #define REG_DEBUG_NEW _RegDebugNew();
  10. #else
  11. #define REG_DEBUG_NEW
  12. #endif // _DEBUG
  13. #endif // _DEBUG_NEW_H_

debug_new.cpp

[c-sharp] view plaincopy
  1. //#include "debug_new.h"
  2. #ifdef _DEBUG
  3. #include
  4. #include
  5. class _CriSec
  6. {
  7. CRITICAL_SECTION criSection;
  8. public:
  9. _CriSec()    { InitializeCriticalSection( &criSection ); }
  10. ~_CriSec()   { DeleteCriticalSection( &criSection );     }
  11. void Enter() { EnterCriticalSection( &criSection );      }
  12. void Leave() { LeaveCriticalSection( &criSection );      }
  13. } _cs;
  14. void _RegDebugNew( void )
  15. {
  16. _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG | _CRTDBG_LEAK_CHECK_DF );
  17. }
  18. void* __cdecl operator new( size_t nSize, const char* lpszFileName, int nLine )
  19. {
  20. // comment 1: MFC中提供的debug new虽然加了锁,但我在实际测试的时候发现多线程并发
  21. //            调用的时候还是抛出了系统错误,所以我在这里加了一个线程互斥量.
  22. // comment 2: debug new和debug delete之间需不需要互斥我并不知道,保险起见,我同样
  23. //            加了线程互斥量.
  24. // comment 3: 按照C++标准规定,在operator new失败后应当调用set_new_handler设置的
  25. //            函数,但是MSDN中却说"头文件new中的set_new_handler是stub的,而应该使
  26. //            用头文件new.h中的_set_new_handler",这简直是滑天下之大稽.
  27. //            以下是VC++6.0中的set_new_handler定义:
  28. //                new_handler __cdecl set_new_handler( new_handler new_p )
  29. //                {
  30. //                    assert( new_p == 0 ); // cannot use stub to register a new handler
  31. //                    _set_new_handler( 0 );
  32. //                    return 0;
  33. //                }
  34. //            所以我也无计可施,只能舍弃set_new_handler的作用.
  35. _cs.Enter();
  36. void* p = _malloc_dbg( nSize, _NORMAL_BLOCK, lpszFileName, nLine );
  37. _cs.Leave();
  38. return p;
  39. }
  40. void __cdecl operator delete( void* p, const char* , int  )
  41. {
  42. _cs.Enter();
  43. _free_dbg( p, _CLIENT_BLOCK );
  44. _cs.Leave();
  45. }
  46. #endif

二、在需要检测的DynamicMem.cpp中加入

1.

#include "debug_new.h"。

2.

 使用调试堆函数:

#define _CRTDBG_MAP_ALLOC 
#include

#include

  注意:#include 语句的顺序。如果更改此顺序,所使用的函数可能无法正确工作。

3.main()中一开始处加入REG_DEBUG_NEW宏

4.在需要检测内存泄漏的地方添加下面这条语句来输出内存泄漏信息:

_CrtDumpMemoryLeaks();

测试实例程序:

DynamicMem.cpp

  1. #include
  2. #include
  3. #include "debug_new.h" // +
  4. #define _CRTDBG_MAP_ALLOC
  5. #include
  6. #include
  7. using namespace std;
  8. int main()
  9. {
  10. REG_DEBUG_NEW; // +
  11. char* name = new char[2];
  12. name[0] = 'A';
  13. name[1] = 'B';
  14. //delete name;
  15. _CrtDumpMemoryLeaks();
  16. cout << "--End--" << endl;
  17. return 0;
  18. }

三、按F5运行。

会在“调试”窗口中显示:

Detected memory leaks!
Dumping objects ->
e:/workspaces/c++/dynamicmem/dynamicmem/dynamicmem.cpp(78) : {120} normal block at 0x003B6360, 2 bytes long.
 Data: 41 42
Object dump complete.

这说明有内存泄漏。

如里把delete[] name;前去掉注释,则“调试”窗口中就不会出现以上的信息,说明无内存泄漏。

以上参考:http://blog.sina.com.cn/s/blog_51396f890102f96e.html

VS2005内存泄漏检测方法[转载]的更多相关文章

  1. VS2005 检测内存泄漏的方法(转载)

    一.非MFC程序可以用以下方法检测内存泄露: 1.程序开始包含如下定义: #ifdef _DEBUG #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __F ...

  2. [教程] Android Native内存泄漏检测方法

    转载请注明出处:https://www.cnblogs.com/zzcperf/p/9563389.html Android 检测 C/C++内存泄漏的方法越来越简便了,下面列举一下不同场景下检测C/ ...

  3. C++程序内存泄漏检测方法

    一.前言 在Linux平台上有valgrind可以非常方便的帮助我们定位内存泄漏,因为Linux在开发领域的使用场景大多是跑服务器,再加上它的开源属性,相对而言,处理问题容易形成“统一”的标准.而在W ...

  4. 【转】简单内存泄漏检测方法 解决 Detected memory leaks! 问题

    我的环境是: XP SP2 . VS2003 最近在一个项目中,程序退出后都出现内存泄漏: Detected memory leaks! Dumping objects -> {98500} n ...

  5. _CrtSetBreakAlloc简单内存泄漏检测方法,解决Detected memory leaks!问题

    我的环境是: XP SP2 . VS2003 最近在一个项目中,程序退出后都出现内存泄漏: Detected memory leaks! Dumping objects -> {98500} n ...

  6. Windows 下的内存泄漏检测方法

    在 Windows 下,可使用 Visual C++ 的 C Runtime Library(CRT) 检测内存泄漏. 首先,我们在.c或.cpp 文件首行插入这一段代码: #define _CRTD ...

  7. 【Visual Studio】简单内存泄漏检测方法 解决 Detected memory leaks! 问题(转)

    原文转自 http://blog.csdn.net/u011430225/article/details/47840647 我的环境是: XP SP2.VS2003 最近在一个项目中, 程序退出后都出 ...

  8. C++ 简单内存泄漏检测方法

    遇到个bug,MFC程序异常退出,debug模式下输出 Detected memory leaks! Dumping objects -> {366566} normal block at 0x ...

  9. C/C++内存泄漏检测方法

    1. 内存泄漏 内存泄漏(Memory Leak)是指程序中已动态分配的堆内存由于某种原因程序未释放或无法释放,造成系统内存的浪费,导致程序运行速度减慢甚至系统崩溃等严重后果. 2. 检测代码 使用链 ...

随机推荐

  1. 使用pscp实现Windows 和 Linux服务器间远程拷贝文件

    转自:http://www.linuxidc.com/Linux/2012-05/60966.htm 在工作中,每次部署应用时都需要从本机Windows服务器拷贝文件到Linux上,有时还将Linux ...

  2. python学习笔记(win32print API介绍)

    最近博主在研究用python控制打印机 这里整理下win32print的API介绍,官网地址http://timgolden.me.uk/pywin32-docs/win32print.html Op ...

  3. 腾讯校招技术岗面试经历及总结(已发offer)

    关于笔试:只要前期复习到位,笔试还是很好过的,但是当然 分数 越高越好,否则后面会被面试官鄙视的.题目可能难度较大,但是要把会做的 都做 对,如果时间比较紧可以适度放弃部分不会的题目. 关于面试: 温 ...

  4. 翻译的技巧(The Technique of Translation)——前言

    曰:你要发文章才能毕业! 答:谨遵圣命-- 缘起 研三了,要找工作了,要离开学校了,又要像浮萍般飘荡在世上了,还是学校好呀!本以为写个毕业论文就可以了,谁知老板幽幽地说写篇文章发了吧.我竟无言以对.不 ...

  5. onenote网页版如何打开链接弄到客户端

    1.网页登录https://onedrive.live.com 2.登录MS帐号 3.输入复制的分享链接,回车 4.点击编辑,在客户端编辑,弹出是否启动应用,点启动,等一段时间完成.

  6. OSGI.NET 学习笔记--应用篇

    关于osgi.net ,想必大家也听说过,以下是自己在学习osgi.net 过程中整理出来的内容,供大家学习参与使用. 1.  OSGI.NET 与UIOSP OSGi是Open Service Ga ...

  7. ICDM 2007

    Language-Independent Set Expansion of Named Entities Using the Web. Chao Wang, Venu Satuluri, Sriniv ...

  8. 洛谷P2738 [USACO4.1]篱笆回路Fence Loops

    P2738 [USACO4.1]篱笆回路Fence Loops 11通过 21提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 ...

  9. Linq把一个DataTable根据一列去除重复数据

    DataTable dt_temp = dt.AsEnumerable().Cast<DataRow>().GroupBy(p => p.Field<string>(&q ...

  10. javascript设计模式-外观模式

    也可译为门面模式.它为子系统中的一组接口提供一个一致的界面, Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用.引入外观角色之后,使用者只需要直接与外观角色交互,使用者与子系统之 ...