// 未释放内存空间.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <string>
#include <iostream>
// 使用CRT调试API
#include <crtdbg.h>
using namespace std;
// 将所有的内存分配函数new替换成CRT提供的调试new
#ifdef _DEBUG
# define DEBUG_CLIENTBLOCK new(_CLIENT_BLOCK, __FILE__, __LINE__)
#else
# define DEBUG_CLIENTBLOCK
#endif
#ifdef _DEBUG
# define new DEBUG_CLIENTBLOCK
#endif
class CTestClass
{
public:
CTestClass(LPWSTR szName)
{
m_lpName = new wstring(szName);
}
~CTestClass()
{
}
void PrintName()
{
wcout << *m_lpName << endl;
}
private:
wstring *m_lpName;
};
HRESULT CreateTestClass(LPWSTR szName, CTestClass **ppObject)
{
*ppObject = new CTestClass(szName);
if ( (*ppObject) == NULL )
return E_FAIL;
else
return S_OK;
}
int _tmain(int argc, _TCHAR* argv[])
{
// 设置CRT调试API的报表输出模式,将所有的错误、警告还有断言都输出到控制台
_CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );
_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT );
_CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT );
CTestClass *pObject = NULL;
HRESULT hr = CreateTestClass(L"This is a Test", &pObject);
if ( hr != S_OK )
{
return -1;
}
else
{
pObject->PrintName();
// 检查未释放的内存
_CrtDumpMemoryLeaks();
return 0;
}
}
|