Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
We continue our modeling of software behaviour with the ubiquitous Memory Leak (process heap) pattern (Volume 1, page 356). Instead of leaking small heap allocations that are easy to debug with user mode stack trace database our model program leaks large heap allocations (Volume 2, page 137):
// MemoryLeak-ProcessHeap
// Copyright (c) 2010 Dmitry Vostokov
// GNU GENERAL PUBLIC LICENSE
// http://www.gnu.org/licenses/gpl-3.0.txt
#include <windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
// create extra 25 heaps initially
for (int i = 0; i < 25; ++i)
HeapCreate(0, 0, 0);
// create a heap to leak within
HANDLE hHeap = HeapCreate(0, 0, 0);
while (true)
{
HeapAlloc(hHeap, 0, 1024*1024);
Sleep(1000);
}
return 0;
}