Share via


Several methods and ideas for Heap leak detection

Heap and Handle allocation happens whilst calling malloc, new, sysallocatestring...

If we forget to release it by calling free, delete, sysfreestring, we experiences the memory leak.

There are several ways to avoid the leaking.

1. Use MS built-in detection API

#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>

_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF )

2. Mark the memory

#define HEAPLEAK(k) k = 0xCACF0000 + __LINE__ // CACF: check allocated check free + line number

3. Hook into the allocate and free functions

_CrtSetAllocHook(pfnHook)

hook to: calloc, malloc, realloc, free, memcpy, strcpy ...

4. Hook from the system level - what NuMega have done before...