使用VC++检查内存泄露(不懂)
来源:互联网 发布:淘宝买灯具便宜吗 编辑:程序博客网 时间:2024/04/29 19:05
通过调试几个C库中的函数,发现利用VC监测内存泄露的问题需要使用几个CRT调试堆函数,似乎非常的麻烦,本来想放弃这个小节的内容的,但是功夫不负有心人,让我在《代码揭秘-从C/C++的角度探秘计算机系统》(左飞 著)一书中找到了相关知识点的描述,但是书中说这些代码来源于网络,不是其本人编写,所以我也无法确认代码的来源,只好使用拿来主义了,在这里感谢前辈们的辛勤付出。 OK,我要贴代码了: //debug_new.h #ifndef _DEBUG_NEW_H_ #define _DEBUG_NEW_H_ #ifdef _DEBUG #undef new extern void _RegDebugNew(void); extern void* __cdecl operator new(size_t, const char*, int); extern void __cdecl operator delete(void*, const char*, int); #define new new(__FILE__, __LINE__) #define REG_DEBUG_NEW _RegDebugNew(); #else #define REG_DEBUG_NEW #endif // _DEBUG #endif // _DEBUG_NEW_H_ 上面是声明部分,下面我给出源文件的代码: // debug_new.cpp #ifdef _DEBUG #include <windows.h> #include <crtdbg.h> class _CriSec { CRITICAL_SECTION criSection; public: _CriSec(){InitializeCriticalSection(&criSection);} ~_CriSec(){DeleteCriticalSection(&criSection);} void Enter(){EnterCriticalSection(&criSection);} void Leave(){LeaveCriticalSection(&criSection);} }_cs; void _RegDebugNew(void) { _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG | _CRTDBG_LEAK_CHECK_DF); } void * __cdecl operator new(size_t nSize, const char* lpszFileName, int nLine) { _cs.Enter(); void *p = _malloc_dbg(nSize, _NORMAL_BLOCK, lpszFileName, nLine); _cs.Leave(); return p; } void __cdecl operator delete(void *p, const char* /*lpszFileName*/, int /*nLine*/) { _cs.Enter(); _free_dbg(p, _CLIENT_BLOCK); _cs.Leave(); } #endif 源码就这么点儿,很小巧吧,下面我说下如何使用: 1、 将上面的两个文件添加到工程中。并在需要检测内存泄露的CPP文件最开始加入如下预定义: #define _CRTDBG_MAP_ALLOC 2、 由于我们要使用CRT调试堆函数,所以宏定义的下方加入如下头文件: #include <stdlib.h> #include <crtdbg.h> 3、 在main函数的开始出加入 REG_DEBUG_NEW; 4、 在代码中调用_CrtDumpMemoryLeaks();函数 这样程序就会在调试输出窗口中显示出内存泄露相关的信息如下图: http://www.cppblog.com/besterChen/archive/2010/01/13/105538.html
- 使用VC++检查内存泄露(不懂)
- VC检查内存泄露
- vc内存泄露检查
- VC++内存泄露检查
- vc检查内存泄露
- VC内存泄露检查方法
- VC内存泄露检查方法
- 使用valgrind检查内存泄露
- vc设置检查内存泄露的方法
- VC内存泄露检查工具:VisualLeakDetector
- •VC内存泄露检查工具:VisualLeakDetector
- VC内存泄露检查工具:VisualLeakDetector
- VC内存泄露检查工具:VisualLeakDetector
- VC内存泄露检查工具:VisualLeakDetector
- VC内存泄露检查工具:VisualLeakDetector
- VC内存泄露检查工具:VisualLeakDetector
- VC内存泄露检查工具:VisualLeakDetector
- VC内存泄露检查工具:VisualLeakDetector
- 计算机视觉文献与代码资源
- 修改Nokia 770的Root Image
- PHP读取数据库中存储的图片
- 多线程程序的一些重要特性
- 汇编语言学习笔记18086指令系统
- 使用VC++检查内存泄露(不懂)
- PHP从mysql中读取并输出二进制文件的乱码问题
- 静态代码块
- Intent属性的设置
- U-Boot的Makefile分析
- 从数学恐慌症中恢复
- 如何使用live writer客户端来发布CSDN的博客文章?
- 烟花
- ActionScript 3.0 杂乱笔记1