您的位置:首页 > 其它

[code].检测内存泄漏 memory detection.

2007-04-19 14:22 176 查看
// MemoryDetection.cpp : Defines the entry point for the console application.
//
////////////////////////////////////////////////////////////////////////////////////////////////////
// 重载的new delete.
//void* operator new(size_t size)
//{
// printf("malloc %u/r/n", size);
// return malloc(size);
//}
//
//void operator delete(void *memblock){
// printf("free/r/n");
// return free(memblock);
////////////////////////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include <crtdbg.h>

///////////////////////////////////////////
// :该函数支持,debug时定位指定内存泄漏处.
// :恢复内存泄漏现场.
// :_CrtSetBreakAlloc();
// :
// :使用方法:
///////////////////////////////////////////

// :支持定位到内存泄漏行
#ifdef _DEBUG
#define new   new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif

// :支持检测内存泄漏.
inline void EnableMemLeakCheck()
{
 _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
}

int _tmain(int argc, _TCHAR* argv[])
{
 EnableMemLeakCheck();
 _CrtSetBreakAlloc(48); // :48是下面的内存分配是第几次内存分配.这里表示 new int[10]是第48次分配内存.
 int* p = new int[10];

 system("pause");
 return 0;
}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leak report delete system file