Using gdb to find memory leaks in HP Unix

The following gdb commands are used to setup memory leak detection in C++ programs:
set heap-check leaks on
set heap-check free on
set heap-check bounds on
set heap-check scramble on

To show the leak the following command is used:

(gdb) info leaks

To view a particular leak from a list of leaks detected use the following:

(gdb) info leak  <leak number> ( leak number is the relevant number from the leak)

It is very important that program be linked with librt.sl shared library to use heap profiling.

The following example is using xscAppAdapter as a C++ program to demonstrate memory leak detection.


1) Set Heap Options

>>gdb xscAppAdapter.gdb
(gdb) set heap-check leaks on
(gdb) set heap-check free on
(gdb) set heap-check bounds on
(gdb) set heap-check scramble on
(gdb) b xscAppAdapter::processMessage

2) Set Breakpoint and Run Application and continue once past the breakpoint to process a message and then detect a leak

Breakpoint 1 at 0x202e4: file xscAppAdapter.C, line 383.
(gdb) run -s PFAppStation -c xsc_Config.8.0 -t xsc_StateManager -r dflt > /dev/null 2>&1 0</dev/null
Starting program: /perf/app/bin/xscAppAdapter.gdb -s PFAppStation -c xsc_Config.8.0 -t xsc_StateManager -r dflt > /dev/null 2>&1 0</dev/null
Breakpoint 1, xscAppAdapter::processMessage (this=0x4081edf8, msg=@0x407b19b0) at xscAppAdapter.C:383
(gdb) c 1
Will stop next time breakpoint 1 is reached.  Continuing.
Breakpoint 1, xscAppAdapter::processMessage (this=0x4081edf8, msg=@0x407b1a50) at xscAppAdapter.C:383
383     in xscOpedsAdapter.C

3) Find leak using info leaks.

(gdb) info leaks
Scanning for memory leaks...
338 bytes leaked in 13 blocks

No. Total bytes Blocks Address Function
0 88 1 0x40510768 operator new(unsigned long)()
1 64 1 0x405321b8 operator new(unsigned long)()
2 37 1 0x4053f1c8 operator new[](unsigned long)()
3 37 1 0x4053f208 operator new[](unsigned long)()
4 31 1 0x406790f0 strdup()
5 22 3 0x4074d548 operator new[](unsigned long)()
6 20 1 0x405f3078 pthread_atfork()
7 15 1 0x40733810 operator new[](unsigned long)()
8 12 1 0x40536510 operator new(unsigned long)()
9 8 1 0x4040e268 operator new(unsigned long)()
10 4 1 0x405efca8 operator new[](unsigned long)()

4) Lookup Individual leak info.

(gdb) info leak 4

Share/Save/Bookmark

Tags: , , , , , , , , , ,

9 Responses to “Using gdb to find memory leaks in HP Unix”

  1. vikas Says:

    nice article this is wat I was looking for

  2. Gayle Points Says:

    I found your blog through a friends blog and this is a great blog to look at some cool things. thanks and bookmarkigng and I am even going to tweet this out to all my friends

  3. Shakia Fertik Says:

    Will be back soon, lovely post, thanks again have bookmarked you!

  4. Gwyn Meigel Says:

    Hallo! Ich habe versucht deinen RSS-Feed zu abonnieren - aber es funktioniert irgendwie nicht. Ich nutze Feedreader. Wieso geht das nicht? Grüße aus Köln

  5. caoni mabi Says:

    9 Good idea. I never thought I would agree with this opinion, but I’m starting to view things from a different view. I have to research more and more on this as it seems very interesting. One thing I don’t understand though is how everything is related together.

  6. Sachin Says:

    I have used gdb several times but not aware of memory leaks detection is so simple. Really a good and useful article.

  7. Mady Says:

    I dont found librt.sl … can some one share librt.sl lib here …

  8. Elmo Swainston Says:

    Greetings from Ohio! I’m bored to death at work so I decided to check out your blog on my iphone during lunch break. I really like the info you present here and can’t wait to take a look when I get home. I’m surprised at how fast your blog loaded on my cell phone .. I’m not even using WIFI, just 3G .. Anyhow, amazing site!

  9. Leonarda Pim Says:

    I have been examinating out many of your posts and i must say pretty nice stuff. I will surely bookmark your blog.

Leave a Reply