Solved

How can I track down a memory leak?

Posted on 2000-02-21
15
184 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
Comment Utility
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
Comment Utility
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
Comment Utility
Does it work with gcc?
0
 
LVL 1

Expert Comment

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

Expert Comment

by:nils pipenbrinck
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 22

Expert Comment

by:nietod
Comment Utility
>>  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
Comment Utility
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
Comment Utility
>>  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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
"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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now