内存分配监控
要在c++程序中,监控内存分配的情况,两个步骤:
1、注册一个hook
_CrtSetAllocHook(YourAllocHook);
注册之后,每次内存分配时,系统都会调用这个hook
2、在hook调用的时候,查看调用堆栈的情况
int YourAllocHook( int allocType, void *userData, size_t size, int
blockType, long requestNumber, const unsigned char *filename, int
lineNumber)
{ static int count = 0;
static int lastSize = 0;
if (allocType == _HOOK_ALLOC) {
<span style="white-space:pre"> </span>dumpStack();
}
return TRUE;
}
查看调用堆栈使用stackwalker
void dumpStack()
{
const UINT max_name_length = 256; // Max length of symbols' name.
CONTEXT context; // Store register addresses.
STACKFRAME64 stackframe; // Call stack.
HANDLE process, thread; // Hand