Debugging Dynamic Memory Usage Errors Using HP WDB (5900-1474; WDB 6.2; January 2011)

Heap Corruption
A heap corruption occurs when an application erroneously overwrites some of the data
in the heap. Heap corruption can result in data corruption, memory corruption, or both.
When an application inadvertently uses the erroneously overwritten data in the heap, it
results in data corruption in the application. Data corruption can lead to unpredictable
program behavior.
The data corruption in the heap can lead to memory corruption if the corrupted data in
the heap is used by memory management functions in the application to allocate, access,
or deallocate memory blocks. In other words, memory corruption occurs when the
corrupted datum in the heap is accessed as a pointer. Memory corruptions compromise
the data integrity of the application and can result in segmentation violations if the
erroneously allocated or accessed memory blocks are out of the bounds of the virtual
memory of the application.
Causes for Heap Corruption
Following are some of the typical causes for heap corruption:
Double-Free
A double-free error occurs when a program attempts to free a memory block that is
already freed. (Example 19 (page 97) illustrates how WDB detects double-frees.)
Freeing Unallocated/Uninitialized Memory
Heap corruption occurs when a program tries to free memory that is not allocated to the
program. Such instances include freeing uninitialized pointers where the pointer addresses
memory outside the allocated memory. (Example 20 (page 98) illustrates how WDB
detects such errors.)
Accessing freed memory
Accessing freed memory results in heap corruption. The scramble feature is a minimal
aid to detect such errors. See “Scrambling a Heap Block (page 54) for more information.
Memory Leaks
A memory leak occurs when an application fails to free allocated memory. As a result,
the kernel frees the memory that is allocated by a process only when the process
terminates. If the program leaks memory on a continual basis, the virtual memory
requirement for the process continues to increase and this can result in serious
consequences for long-running applications and memory intensive applications.
Memory leaks can also cause fragmentation of the heap. This slows down the allocation,
de-allocation, and access of memory blocks and can eventually cause the application
to fail with out-of-memory errors.
8