Debugging Dynamic Memory Usage Errors Using HP WDB v6.3 (5900-2181, August 2012)
library routines. For more information on the dependent standard library routines, see the HP
WDB release notes, available at the HP WDB Documentation website at:
http://www.hp.com/go/wdb
Memory-Related Errors
This section discusses the following memory-related errors that can occur in an application:
• Heap corruption
• Memory leaks
• Access errors
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 76) 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 77) 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 44) 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.
When to Suspect a Memory Leak?
You must suspect a memory leak in an application if the system runs out of swap space, runs slower,
or both. Memory leaks in an application increase the memory consumption in an application.
Memory-Related Errors 7