Detecting heap usage error
Description
Checks that the heap interface—malloc, new, free, etc—is used properly by your application. The following improper uses are checked for:
Using the incorrect deallocator—
free,delete, etc—for an allocator—malloc,new, etc. For example:char * p1 = (char *)malloc(23); /* Allocation using malloc. */ char * p2 = new char[23]; /* Allocation using new[]. */ char * p3 = new int; /* Allocation using new. */ delete p1 /* Error, allocated using malloc. */ free(p2); /* Error, allocated using new[]. */ delete[] p3; /* Error, allocated using new. */
Freeing a heap block more than once.
Trying to allocate a heap block that is too large.
Why perform the check
To verify that the heap interface is used correctly.
How to use it
Linker option: ‑‑debug_heap
In the IDE: Project>Options>Runtime Checking>Use checked heap
The checked heap will replace the normal heap for the whole application. The checked heap requires extra heap and stack resources. Make sure that your application has at least 10 Kbytes of heap and 4 Kbytes of stack.
The limit for how large a heap block can be at allocation is by default 1 Gbyte. The limit can be changed by the function:
size_t __iar_set_request_report_limit(size_t value);
The function returns the old limit. You can find the declaration of this function in iar_dlmalloc.h. For more information, see iar_dlmalloc.h.
How it works
For any incorrect use of the heap interface, a message will be issued.
See also The checked heap provided by the library.
Example
Follow the procedure described in Getting started using C-RUN runtime error checking, but use the Debug heap option.
This is an example of source code that will be identified during runtime:
C-RUN will report either Heap integrity violation or Heap usage error. This is an example of the message information that will be listed: