📄 memtrace.hpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#pragma once
#ifdef DEBUG
#include <hash_map.hxx>
struct _MemInfo_t
{
char FileName[25];
char FunctionName[50];
int LineNum;
size_t SizeAllocated;
};
typedef ce::hash_map<void*, _MemInfo_t*> NewDeleteMap;
extern NewDeleteMap __g_NewDeleteMap;
inline
void*
_DoAlloc(
size_t ToAlloc,
const char* pFileName,
const char* pFunctionName,
int LineNum
)
{
void* pToReturn = LocalAlloc(LMEM_ZEROINIT, ToAlloc);
if (pToReturn)
{
_MemInfo_t* pInfo = (_MemInfo_t*)LocalAlloc(LMEM_ZEROINIT, sizeof(_MemInfo_t));
if (pInfo)
{
const char* PostPathFileName = strrchr(pFileName, L'\\');
if (! PostPathFileName)
{
PostPathFileName = pFileName;
}
else
{
PostPathFileName++;
}
StringCchCopyA(pInfo->FileName, _countof(pInfo->FileName), PostPathFileName);
StringCchCopyA(pInfo->FunctionName, _countof(pInfo->FunctionName), pFunctionName);
pInfo->LineNum = LineNum;
pInfo->SizeAllocated = ToAlloc;
__g_NewDeleteMap.insert(
NewDeleteMap::value_type(
pToReturn,
pInfo
)
);
}
}
return pToReturn;
}
inline
void*
operator new(
size_t ToAlloc,
const char* pFileName,
const char* pFunctionName,
int LineNum
)
{
return _DoAlloc(ToAlloc, pFileName, pFunctionName, LineNum);
}
inline void _DoDelete(void* pToDelete)
{
NewDeleteMap::iterator it = __g_NewDeleteMap.find( pToDelete );
if (it != __g_NewDeleteMap.end())
{
_MemInfo_t* pInfo = it->second;
LocalFree (pInfo);
__g_NewDeleteMap.erase(it);
}
LocalFree(pToDelete);
}
inline void operator delete(void* pToDelete)
{
_DoDelete(pToDelete);
}
inline void operator delete[] (void* pToDelete)
{
_DoDelete(pToDelete);
}
inline void _MemTrackCheck()
{
NewDeleteMap::iterator it = __g_NewDeleteMap.begin();
while (it != __g_NewDeleteMap.end())
{
_MemInfo_t* pInfo = it->second;
DEBUGMSG(1, (L"Leaked %d bytes:\tFile:%S\tFunction:%S\tLine Number:%d",
pInfo->SizeAllocated,
pInfo->FileName,
pInfo->FunctionName,
pInfo->LineNum
));
delete pInfo;
++it;
}
if (__g_NewDeleteMap.begin() != __g_NewDeleteMap.end())
{
ASSERT(0);
}
}
#define New new(__FILE__, __FUNCTION__, __LINE__)
#define new New
#define MemTrackCheck() _MemTrackCheck()
#define MemTrackInitialize() NewDeleteMap __g_NewDeleteMap;
#define TrackAlloc(size) _DoAlloc(size, __FILE__, __FUNCTION__, __LINE__)
#define TrackFree(p) _DoDelete(p)
#else
#define MemTrackCheck()
#define MemTrackInitialize()
#define TrackAlloc(s) LocalAlloc(LMEM_ZEROINIT, s)
#define TrackFree(p) LocalFree(p)
#endif //ifdef DEBUG
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -