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
Tags: breakpoint, c++, gdb, heap profiling, heap-check, hp unix, info leaks, librt.sl, linkedin, memory leak, performance
May 10th, 2009 at 9:03 pm
nice article this is wat I was looking for
October 5th, 2010 at 9:02 am
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
October 6th, 2010 at 1:49 am
Will be back soon, lovely post, thanks again have bookmarked you!
May 25th, 2011 at 8:00 am
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
September 30th, 2011 at 7:33 pm
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.
December 17th, 2011 at 2:07 pm
I have used gdb several times but not aware of memory leaks detection is so simple. Really a good and useful article.
January 5th, 2012 at 11:36 pm
I dont found librt.sl … can some one share librt.sl lib here …
January 9th, 2012 at 3:35 am
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!
January 9th, 2012 at 7:18 am
I have been examinating out many of your posts and i must say pretty nice stuff. I will surely bookmark your blog.