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

Example 24 Detecting memory leaks when you free a structure or an array that has
pointers which are not freed.
Sample Program
$ cat memleak3.c
1 #include<stdio.h>
2
3 struct stud
4 {
5 char* name;
6 int id;
7 };
8
9 int main() {
10
11 struct stud *s1;
12 s1 = (struct stud*)malloc(sizeof(struct stud));
13 s1->name = (char*)malloc(50);
14 strcpy(s1->name,"Annie");
15 s1->id=10;
16 free(s1);
17 }
Sample Debugging Session
(gdb) set heap-check on
(gdb) file memleak3
Reading symbols from memleak3...done.
(gdb) b 17
Breakpoint 1 at 0x2c3c: file memleak3.c, line 17 from memleak3.
(gdb) r
Starting program:memleak3
Breakpoint 1, main () at memleak3.c:17
17 }
(gdb) info leak
Scanning for memory leaks...
50 bytes leaked in 1 blocks
No. Total bytes Blocks Address Function
0 50 1 0x4042a3e0 main()
(gdb) info leak 0
50 bytes leaked at 0x4042a3e0 (100.00% of all bytes leaked)
#0 main() at memleak3.c:13
#1 _start() from /usr/lib/libc.2
#2 _start() from /opt/langtools/lib/librtc.sl
#3 $START$() from usr/lib/libc.2
104