Format
#include <stdlib.h> /* also in <malloc.h> */ void _debug_free(void *ptr, const char *file, size_t line);
Language Level: Extension
_debug_free is the debug version of free. Like free, it frees the block of memory pointed to by ptr. _debug_free also sets each block of freed memory to 0xFB, so you can easily locate instances where your program uses the data in freed memory.
In addition, _debug_free makes an implicit call to the _heap_check, and stores the file name file and the line number line where the memory is freed. This information can be used later by the _heap_check, _dump_allocated, or _dump_allocated_delta functions.
To use _debug_free, you must compile with the debug memory -qheapdebug compiler option. This option maps all free calls to _debug_free.
Note: The -qheapdebug option maps all calls to memory management functions (including heap-specific versions) to their debug counterparts. To prevent a call from being mapped, parenthesize the function name.
Because _debug_free always checks what heap the memory was allocated from, you can use _debug_free to free memory blocks allocated by the regular, heap-specific, or debug versions of the memory management functions. However, if the memory was not allocated by the memory management functions, or was previously freed, _debug_free generates an error message and the program ends.
Return Value
There is no return value.
Example
This example reserves two blocks, one of 10 bytes and
the other of 20 bytes. It then frees the first block and attempts
to overwrite the freed storage. When _debug_free
is called a second time, _heap_check detects the
error, prints out several messages, and stops the program.
Note: You must compile this example with the -qheapdebug option to map the free calls to _debug_free.
#include <stdlib.h> #include <stdio.h> #include <string.h>
int main(void) { char *ptr1, *ptr2;
if (NULL == (ptr1 = (char*)malloc(10)) || NULL == (ptr2 = (char*)malloc(20))) { puts("Could not allocate memory block."); exit(EXIT_FAILURE); } free(ptr1); memset(ptr1, 'a', 5); /* overwrites storage that has been freed */ free(ptr2); /* this call to free invokes _heap_check */ puts("_debug_free did not detect that a freed memory block was overwritten."); return 0;
/**************************************************************************** The output should be similar to:
Free heap was overwritten at 0x00073890. Heap state was valid at line 12 of _debug_free.c. Memory error detected at line 14 of _debug_free.c. ****************************************************************************/ }
Debugging Memory Heaps
Memory Management Functions
Managing Memory with Multiple
Memory Heaps
Debugging Problems with Heap
Memory
-qheapdebug Compiler
Option
_debug_calloc - Allocate and Initialize
Memory
_debug_heapmin - Free Unused Memory in the
Default Heap
_debug_malloc - Allocate Memory
_debug_memcpy - Copy Bytes
_debug_memmove - Copy Bytes
_debug_memset - Set Bytes to Value
_debug_realloc - Reallocate Memory Block
_debug_strcat - Concatenate Strings
_debug_strcpy - Copy Strings
_debug_strncat - Concatenate Strings
_debug_strncpy - Copy Strings
_debug_strnset - Set Characters in String
_debug_strset - Set Characters in String
_debug_ucalloc - Reserve and Initialize
Memory from User Heap
_debug_uheapmin - Free Unused Memory in
User Heap
_debug_umalloc - Reserve Memory Block from
User Heap