HeapFree function
Applies to: desktop apps | Metro style apps
Frees a memory block allocated from a heap by the HeapAlloc or HeapReAlloc function.
Syntax
BOOL WINAPI HeapFree( __in HANDLE hHeap, __in DWORD dwFlags, __in LPVOID lpMem );
Parameters
- 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.
- dwFlags [in]
-
The heap free options. Specifying the following value overrides the corresponding value specified in the flOptions parameter when the heap was created by using the HeapCreate function.
Value Meaning - HEAP_NO_SERIALIZE
- 0x00000001
Serialized access will not be used. For more information, see Remarks.
To ensure that serialized access is disabled for all calls to this function, specify HEAP_NO_SERIALIZE in the call to HeapCreate. In this case, it is not necessary to additionally specify HEAP_NO_SERIALIZE in this function call.
Do not specify this value when accessing the process heap. The system may create additional threads within the application's process, such as a CTRL+C handler, that simultaneously access the process heap.
- lpMem [in]
-
A pointer to the memory block to be freed. This pointer is returned by the HeapAlloc or HeapReAlloc function. If this pointer is NULL, the behavior is undefined.
Return value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. An application can call GetLastError for extended error information.
Remarks
You should not refer in any way to memory that has been freed by HeapFree. After that memory is freed, any information that may have been in it is gone forever. If you require information, do not free memory containing the information. Function calls that return information about memory (such as HeapSize) may not be used with freed memory, as they may return bogus data. Calling HeapFree twice with the same pointer can cause heap corruption, resulting in subsequent calls to HeapAlloc returning the same pointer twice.
Serialization ensures mutual exclusion when two or more threads attempt to simultaneously allocate or free blocks from the same heap. There is a small performance cost to serialization, but it must be used whenever multiple threads allocate and free memory from the same heap. Setting the HEAP_NO_SERIALIZE value eliminates mutual exclusion on the heap. Without serialization, two or more threads that use the same heap handle might attempt to allocate or free memory simultaneously, likely causing corruption in the heap. The HEAP_NO_SERIALIZE value can, therefore, be safely used only in the following situations:
- The process has only one thread.
- The process has multiple threads, but only one thread calls the heap functions for a specific heap.
- The process has multiple threads, and the application provides its own mechanism for mutual exclusion to a specific heap.
Examples
For an example, see Getting Process Heaps.
Requirements
|
Minimum supported client | Windows XP |
|---|---|
|
Minimum supported server | Windows Server 2003 |
|
Header |
|
|
Library |
|
|
DLL |
|
See also
Send comments about this topic to Microsoft
Build date: 2/7/2012
Prior to Windows Vista, HeapFree has a bug: only the low byte of the return value is correctly indicative of the result. This is because the implementation returns type BOOLEAN (BYTE) despite the prototype declaring it as returning BOOL (int).
If you care about the return value of HeapFree, and you need to support XP and 2003, cast the return value to BOOLEAN before checking it.
Thanks to Geoff Chappell for pointing this out.
- 10/12/2011
- Myria