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

Example 23 Detecting memory leaks that are caused when a pointer variable in an
application addresses memory that is out of the scope of the application
Sample Program
$ cat memleak2.c
1 #include<stdio.h>
2
3 void func1(int* ptr1)
4 {
5 ptr1 = (int*)malloc(5*sizeof(int));
6 }
7
8 void func2(int** ptr)
9 {
10 func1(*ptr);
11 }
12 int main()
13
14 {
15 printf("Starting program\n");
16 int* han1;
17 func2(&han1);
18 printf("End of the program\n");
19 }
Sample Debugging Session
(gdb) set heap-check on
(gdb) file memleak2
Reading symbols from memleak2...done.
(gdb) b 19
Breakpoint 1 at 0x2c4c: file memleak2.c, line 19 from memleak2.
(gdb) r
Starting program: memleak2
Starting program
End of the program
Breakpoint 1, main () at memleak2.c:19
19 }
(gdb) info leak
Scanning for memory leaks...
20 bytes leaked in 1 blocks
No. Total bytes Blocks Address Function
0 20 1 0x4042c3d8 func1()
(gdb) info leak 0
20 bytes leaked at 0x4042c3d8 (100.00% of all bytes leaked)
#0 func1() at memleak2.c:5
#1 func2() at memleak2.c:10
102