一种简单的Visual C++中内存泄漏的检测方法

来源:互联网 发布:爆破手机号软件 编辑:程序博客网 时间:2024/05/21 22:44


#define _CRTDBG_MAP_ALLOC
#include<stdlib.h>
#include<crtdbg.h>

#ifdef _DEBUG


#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)


#endif

inline void EnableMemLeakCheck(){
    _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
}


int main() {
    EnableMemLeakCheck();

    int* a = new int[100];

    return 0;
}

 

/*
输出窗口会输出以下内容,点击还可以定位到 “int* a = new int[100];”这一行:
Detected memory leaks!
Dumping objects ->
c:\documents and settings\liuxiaobo\my documents\visual studio 2008\projects\test\test\main.cpp(21) : {56} normal block at 0x003D4EB0, 400 bytes long.
Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
*/