### _debug_free -- Release Memory

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 and _dump_allocated or _dump_allocated_delta functions.

To use _debug_free, you must compile with the debug memory (/Tm) compiler option. This option maps all free calls to _debug_free.

Note: The /Tm option maps all calls to memory management functions (including a heap-specific version and a tiled version for OS/2) 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, tiled (for OS/2) 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 /Tm 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.
       ****************************************************************************/
    }

Managing Memory with Multiple Heaps
Memory Management

_debug_calloc -- Allocate and Initialize Memory
_debug_malloc -- Allocate Memory
_debug_realloc -- Reallocate Memory Block
_dump_allocated -- Get Information about Allocated Memory
_dump_allocated_delta -- Get Information about Allocated Memory
free -- Release Storage Blocks
_heap_check -- Validate Default Memory Heap
<malloc.h>
<stdlib.h>
/Tm compiler option