Solved

How can I track down a memory leak?

Posted on 2000-02-21
15
187 Views
Last Modified: 2013-12-14
I have reason to believe that the code that
I am dealing with has a memory leak in
it, on the system that I am using (a fairly old
gcc compiler/library).

How can I track this down?

Ken
P.S.  Many of you will undoubtably advise me
to switch to a newer compiler.  I know that I
should but I have reasons for not doing so
at this point.
0
Comment
Question by:klopter
  • 7
  • 4
  • 2
  • +2
15 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2543443
Answer:  switch to a new compiler.

Sorry.

One way would be to overload new and delete to record each allocation in a table along with the line number and file name of each allocation and remove these entries on each deletion.  (This is someting that modern compilers can often do for you...)  This is quite a bit of work though.  (Well, not that bad, but it isn't a quick fix.)
0
 
LVL 1

Expert Comment

by:snifong
ID: 2543469
Download a trial version of Purify. http://www.rational.com/products/purify_nt/tryit/index.jtmpl  It would gove you 14 days to work with it.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2543538
Does it work with gcc?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:snifong
ID: 2543623
Hmmm, good point.  I'm not sure.
0
 
LVL 4

Expert Comment

by:nils pipenbrinck
ID: 2543851
I usually do this with some lines of assembler code.. that's a little bit tricky, but gives GREAT results.


I simply write a new malloc and free routine which takes the same parameters as the old malloc & free.

then I change the names of malloc and free in the library using a hex-editor.. someting like mallox and frex usually work.

in my new malloc I call mallox to get the memory with some extra bytes.

in these bytes I store a linked list of all allocated objects along with the caller address (I get these directly from the stack using some asm lines and a little bit try % error).

in my free I first remove the  memory-block from my linked list (a nice spot to find out if a memory block has been freed twice btw).

at the end of the program you can walk down the linked list and print out the size and caller address of all non-freed memory blocks.

take your map-file and you'll find exactly in which lines/routines the memory wasn't freed.

it may take a day or two to get this working, but after you did it you have one of the best heap debugging systems you can have...

I do this with watcom c++ under dos & windows, but it works with any compiler.


happy coding,
  Nils

-------------------
  save the whales!
  feed the hungry!
  free the mallocs!
0
 
LVL 22

Expert Comment

by:nietod
ID: 2543860
I think if you try what I suggest,  you would it easier and portable and you don't have to use a map file (which can be a bit tough when you have an application that is upwards of a few 100K).
0
 
LVL 4

Expert Comment

by:nils pipenbrinck
ID: 2544109
nietod..

that would work.. agreed.

but it won't track errors in the stdlib (which still use malloc and free), and won't also work for third party libraries.

nils


0
 
LVL 22

Expert Comment

by:nietod
ID: 2544578
>>  track errors in the stdlib (which still use
>> malloc and free), and won't also work
>> for third party libraries.
True but in my experience all bugs are caused by only one programmer--me.  I don't know if that is your experience or not.  (If it is, you must really hate me. :-)   )

actuallly this approach will work to track errors in the STL lib too, just not in the compiled library code.
0
 
LVL 4

Expert Comment

by:nils pipenbrinck
ID: 2545314
well, I guess it's fair when I withdraw my answer. maybe it's to harcore for the average programmer.

nietod: I agree, the stl is mostly bugfree. I work most of the time with watcom C, the stl has memory leaks, but I can live with them (below 1 k total.. mostly i/o buffers). However, I do programming on game consoles, and we have to use third party libraries and libs, that are made that fast, that they haven't been really tested. you won't beleave what shitty code you have to deal with (memory leaks included).

Nils
0
 
LVL 22

Expert Comment

by:nietod
ID: 2545758
>>  harcore for the average programmer.
I was a full-time assembly language programmer for 10+ years and still do a few hours of assembly every week.  (currently full time again) and I find it too hardore.  But as you said it does have an advantage or two that my approach does not.  
0
 
LVL 4

Expert Comment

by:nils pipenbrinck
ID: 2545864
you do assembly programming? nietod, you answer to almost any question I ask.. could you please tell me where do you get the time to do so? :)

Assembler is my favorite language.. from time to time I also can use it at work, but most of the stuff I do is just for home use..

ehm.. I'm gettig quite offtopic.. better I'll stop now.

Nils
0
 
LVL 22

Expert Comment

by:nietod
ID: 2546013
answer: I don't do anything else.  

(I check out EE while I'm waiting for compiles/asembles/tests etc to finish.)
0
 
LVL 1

Accepted Solution

by:
ntdragon earned 100 total points
ID: 2549564
if you use class's then just
add a static data member num and
in the c'tor do num++
and in the d'tor do num--;

if you have questions about how to make this static data member send me an e-mail.
0
 

Author Comment

by:klopter
ID: 2549822
I am sorry.  I don't know what c'tor
or d'tor means.

Ken

nils, I am comfortable with assembly,
I even comtemplated your solution,
but I was hoping for a simpler solution.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2550037
"c'tor" is "constructor and (need I say it?) "d'tor" is "destructor"

He is proposing adding static data members to each class that counts the number of instances of each class.  When the program ends the number of instances should be 0.  The problem with this is that a) it won't detect non-object based based memory leaks (which may not be a problem and b) it is hard to learn the value of these counters at program termination--this is a problem.  I have fooled with solutions to the second part and never really came up with an answer i really liked.  Although for me alot of the problem had to do with DLLs.  Do you have DLLs?

The solution I proposed will be simpler than nil's but provides slightly less coverage (which is now annoying me because I begin to wonder about my program...).  it is completely standard C++ so there is no assembly or portability problems, however it still is not _simple_.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question