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

d. Step into the next calling function i.e. the createString() and collect leak
information with the info leaks command when the execution reaches there.
NOTE: The leak information is not shown as the hdrString is pointing to the
allocated memory of size 20.
(gdb) finish
Run till exit from #0 0x4000e70:0 in createHeader () at memLeak.c:24
0x4000f40:0 in createString (input=0x4000bc0 "String 1", n=25) at memLeak.c:37
37 hdrString = createHeader();
Value returned is $2= 0x400124a0 "Header : My string "
(gdb) info leak
Scanning for memory leaks...
No new leaks.
(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
Conclusion 95