26#if defined(_DEBUG) && !defined(_WIN32_WCE)
47 #define _TnbStrFmt sprintf
49 #define _TnbStrFmt sprintf_s
57 _declspec(selectany) _CRT_ALLOC_HOOK gpAllocHook = NULL;
59 struct CCrtDbgAutoCheck
62 CCrtDbgAutoCheck(
void)
64 int tmpFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
65 tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
66 _CrtSetDbgFlag(tmpFlag);
68 if ( gpAllocHook == NULL )
71 gpAllocHook = _CrtSetAllocHook(MyAllocHook);
76 static int __cdecl MyAllocHook(
int iAllocType, LPVOID P,
size_t nSize,
77 int iBlockType,
long lRequestNo,
const unsigned char* lpszFile,
int iLine)
79 if ( iAllocType == _HOOK_FREE && iBlockType == _NORMAL_BLOCK )
81 long* L =
static_cast<long*
>(P);
83 _TnbStrFmt(buf,
"mem{%d} Free / addr=0x%08X / size=%d\n", L[-2], P, L[-4]);
84 ::OutputDebugStringA(buf);
91 inline LPVOID MemAlloc(
size_t nSize, LPCSTR lpszFileName,
int nLine)
93 static TNB::NEW::CCrtDbgAutoCheck sAutoCheck;
94 LPVOID P = _malloc_dbg(nSize, _NORMAL_BLOCK, lpszFileName, nLine);
99 ::_CrtIsMemoryBlock(P, nSize, &lRequestNo, NULL, NULL);
101 _TnbStrFmt(buf,
"%s(%d): mem{%d} alloc\n", lpszFileName, nLine, lRequestNo);
102 ::OutputDebugStringA(buf);
103 _TnbStrFmt(buf,
"mem{%d} Alloc / addr=0x%08X / size=%d\n", lRequestNo, P, nSize);
104 ::OutputDebugStringA(buf);
109 _TnbStrFmt(buf,
"%s(%d): mem.alloc error\n", lpszFileName, nLine);
110 ::OutputDebugStringA(buf);
115 _CrtDbgReport(_CRT_ERROR, lpszFileName, nLine, NULL,
"メモリ確保に失敗しました。");
116 throw std::bad_alloc();
126inline LPVOID __cdecl
operator new(
size_t nSize, LPCSTR lpszFileName,
int nLine)
128 return TNB::NEW::MemAlloc(nSize, lpszFileName, nLine);
134inline void __cdecl
operator delete(LPVOID P, LPCSTR lpszFileName,
int nLine)
136 _free_dbg(P, _NORMAL_BLOCK);
142 #define DEBUG_NEW new(__FILE__, __LINE__)
145 #define new DEBUG_NEW
147#define malloc(SIZE) TNB::NEW::MemAlloc(SIZE, __FILE__, __LINE__)
PlacementNew/Delete関係のヘッダ