c++中捕捉内存泄露、异常

//在Watch面板加上可以观察当前断点处最后一条异常信息:@err,hr

#include "stdafx.h" #include <iostream> using namespace std; #ifdef _DEBUG #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__) #else #define DEBUG_CLIENTBLOCK #endif #define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> #include <string.h> #include <stdio.h> #ifdef _DEBUG #define new DEBUG_CLIENTBLOCK #endif int _tmain(int argc, _TCHAR* argv[]) { _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);      //_CrtSetBreakAlloc(52);
char* d; int *p; d= (char*)malloc(100); p = new int; /***这两行注释与不注释结果不一样 free(d); delete p; ***/ _CrtDumpMemoryLeaks(); system("pause"); return 0; }

  在Visual C++ 中,可以在监视窗口添加 $err,hr
一行来实时现实错误。调试过程中,该项相当于在每次调用API函数之后调用GetLastError函数。其值由两部分组成,一个是错误代码(十六进制),另一个是错误代码所对应的文本提示。该方法支持多语言

posted on 2012-04-01 20:21  chuncn  阅读(2637)  评论(0编辑  收藏  举报

导航