Debugging Dynamic Memory Usage Errors Using HP WDB (766161-001, March 2014)

(gdb)
e. Step into the compareStrings() and collect leak information with the info leaks
command. If the leak is reported, it displays that the pointer to the memory allocated in
the allocateMemory() function is lost before getting into the compareStrings()
function.
(gdb) finish
Run till exit from #0 0x4000f40:0 in createString (
input=0x4000bc0 "String 1", n=25) at memLeak.c:37
Too small size for a string, not allocating
Number of badInput is 1
0x40012c0:0 in compareStrings () at memLeak.c:74
74 str1 = createString("String 1", s1length);
Value returned is $3= 0x0
(gdb) info leak
Scanning for memory leaks...
20 bytes leaked in 1 blocks
No. Total bytes Blocks Address Function
0 20 1 0x400124a0 allocateMemory()
Observe that the pointer is lost while returning from the createString() function to the
compareStrings() function. This is because, the program does not free the memory in the
createString() function before the exit using the hdrString pointer when n is less than
(inputLength + 20).
To fix this memory leak, you must free the memory allocated, in the createString() function
by calling the free for hdrString even when n is less than (inputLength + 20).
Conclusion
Memory-related errors are some of the most difficult programming errors to detect and debug.
Debugging memory-related errors is difficult without the help of an effective memory analysis tool.
WDB enables you to debug memory leaks and heap-related errors in an application.
In addition to plugging memory leaks in your application, it is also important to track the memory
utilization in your application. WDB provides capabilities such as heap profiling and error injection
to analyze the memory-usage of your application. The heap profile displays information about the
allocated memory, the calling function, and it also displays the allocating call stack.
Additional Examples
Example 20 to Example 27 illustrate how WDB detects memory leaks and heap-errors caused by
different types of programming errors.
82