在VS2008中检测内存泄漏

来源:互联网 发布:c语言malloc函数用法 编辑:程序博客网 时间:2024/05/22 06:54
在VS2008中需要增加几句代码来显示C++程序中的内存泄露,代码如下:

#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#ifdef _DEBUG
#define new new(_NORMAL_BLOCK,__FILE__,__LINE__)
#endif

int main()
{
    /*         其他代码      */

    _CrtDumpMemoryLeaks();
    return 0 ;14:
}

在使用DEBUG时,程序退出后就会自动报告是否有内存泄漏!

注意,一定要在程序最后加入(在 return 前面)

原创粉丝点击