Search code examples
visual-c++nullheap-memoryfree

HeapFree function - Null parameter?


The Windows HeapFree function has the following signature:

BOOL WINAPI HeapFree(
  _In_ HANDLE hHeap,
  _In_ DWORD  dwFlags,
  _In_ LPVOID lpMem
);

In the documentation there is not mention about what is supposed to happen if hHeap is null.

https://msdn.microsoft.com/en-us/library/windows/desktop/aa366701(v=vs.85).aspx


Solution

  • The documentation for HeapFree says:

    hHeap [in] A handle to the heap whose memory block is to be freed. This handle is returned by either the HeapCreate or GetProcessHeap function.

    You must pass a valid heap handle. NULL is not a valid heap handle (both HeapCreate and GetProcessHeap indicate failure by returning NULL). If you do not pass a valid heap handle, the behavior is undefined.