_heap_walk -- Return Information about Default Heap

Format

#include <malloc.h>
int _heap_walk(int (*callback_fn)(const void *object, size_t size,
                                              int flag, int status,
                                              const char* file, int line) );

Language Level: Extension
_heap_walk traverses the default heap, and for each allocated or freed object, it calls the callback_fn function that you provide. For each object, it passes your function:

object A pointer to the object.
size The size of the object.
flag The value _USEDENTRY if the object is currently allocated, or _FREEENTRY if the object has been freed. (Both values are defined in <malloc.h>.)
status One of the following values, defined in <malloc.h>, depending on the status of the object:
_HEAPBADBEGIN The heap specified is not valid; it may have been closed or destroyed.
_HEAPBADNODE A memory node is corrupted, or the heap is damaged.
_HEAPEMPTY The heap has not been initialized.
_HEAPOK The heap appears to be consistent.
file The name of the file where the object was allocated.
line The line where the object was allocated.

_heap_walk provides information about all objects, regardless of which memory management functions were used to allocate them. However, the file and line information are only available if the object was allocated and freed using the debug versions of the memory management functions. Otherwise, file is NULL and line is 0.

_heap_walk calls callback_fn for each object until one of the following occurs:

You may want to code your callback_fn to return a nonzero value if the status of the object is not _HEAPOK. Even if callback_fn returns 0 for an object that is corrupted, _heap_walk cannot continue because of the state of the heap and returns to its caller.

You can use callback_fn to process the information from _heap_walk in various ways. For example, you may want to print the information to a file or use it to generate your own error messages. You can use the information to look for memory leaks and objects incorrectly allocated or freed from the heap. It can be especially useful to call _heap_walk when _heapchk returns an error.

A heap-specific version of this function, _uheap_walk, is also available.

Notes:

  1. Using the _heapchk, _heapset, and _heap_walk functions (and their heap-specific equivalents) may add overhead to each object allocated from the heap.
  2. _heap_walk locks the heap while it traverses it, to ensure that no other operations use the heap until _heap_walk finishes. As a result, in your callback_fn, you cannot call any critical functions in the runtime library, either explicitly or by calling another function that calls a critical function.

Return Value
_heap_walk returns the last value of status to the caller.

Example



Managing Memory with Multiple Heaps
Memory Management


_heapchk -- Validate Default Memory Heap
_heapmin -- Release Unused Memory from Default Heap
_heapset -- Validate and Set Default Heap
_uheap_walk -- Return Information about Memory Heap
<malloc.h>