📄 memtracking.cpp
字号:
//
// 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.
//
#include "MemTracking.h"
#include "vector.hxx"
#include "DebugMsg.h"
#include "string.hxx"
#ifdef DEBUG
struct _Bucket
{
public:
ce::string c_strBucketName;
UINT cAllocs;
UINT cDeallocs;
_Bucket()
{
cAllocs = 1;
cDeallocs = 0;
}
BOOL Balanced()
{
return (cAllocs == cDeallocs);
}
};
typedef ce::vector<_Bucket *> BucketTable;
static BucketTable __s_BucketTable;
VOID _MemTrackAdd(const CHAR* c_szFunction)
{
const CHAR *szAfterScope = strchr(c_szFunction, ':');
PREFAST_ASSERT(szAfterScope != NULL && *(szAfterScope + 1) == ':');
szAfterScope += 2;
BucketTable::iterator it = __s_BucketTable.begin();
while (it != __s_BucketTable.end())
{
if (strcmp((*it)->c_strBucketName, szAfterScope) == 0)
{
break;
}
it++;
}
if (it != __s_BucketTable.end())
{
(*it)->cAllocs++;
}
else
{
_Bucket *pBucket = new _Bucket();
PREFAST_ASSERT(pBucket);
pBucket->c_strBucketName = szAfterScope;
__s_BucketTable.push_back(pBucket);
}
}
VOID _MemTrackRemove(const CHAR* c_szFunction)
{
const CHAR *szAfterScope = strchr(c_szFunction, ':');
PREFAST_ASSERT(szAfterScope && *(szAfterScope + 1) == ':' && *(szAfterScope + 2) == '~');
szAfterScope += 3;
BucketTable::iterator it = __s_BucketTable.begin();
while (it != __s_BucketTable.end())
{
if (strcmp((*it)->c_strBucketName, szAfterScope) == 0)
{
break;
}
it++;
}
ASSERT(it != __s_BucketTable.end());
(*it)->cDeallocs++;
}
VOID _MemTrackCheck()
{
BucketTable::iterator it = __s_BucketTable.begin();
UINT cTotalAllocs = 0;
while (it != __s_BucketTable.end())
{
DEBUGMSG(ZONE_OWAEC_TRACING_ALLOCS, (L"%d\t allocations for %S", (*it)->cAllocs, (const CHAR*)(*it)->c_strBucketName));
cTotalAllocs += (*it)->cAllocs;
if(!(*it)->Balanced())
{
DEBUGMSG(ZONE_OWAEC_ERROR, (L"\t\t%S had\t only %d deallocations!", (const CHAR*)(*it)->c_strBucketName, (*it)->cDeallocs));
ASSERT(FALSE);
}
delete (*it);
it++;
}
DEBUGMSG(ZONE_OWAEC_TRACING_ALLOCS, (L"%d total owaexchangeclient object allocations", cTotalAllocs));
__s_BucketTable.clear();
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -