DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2005
    Posts
    134

    Looking for Memory Leak Advice

    Apart from buying BoundsChecker or any of the other tools to track down memory leaks. Does anyone have any advice or tips from your years of programming experience to offer on how to track down the causes of memory leaks?

    I find that the big dump that happens at the end of a program does not typically lend any useful clues as to where or what was allocated that was not freed. I find that I have to back track and comment out sections of code until through the process of elimination and trial-and-error, I am able to narrow down where the problem is. And since this is usually at the bottom of the To-Do list, the memory leaks are not plugged.

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    if the program is interactive, you can bring up a tool like the task manager in windows or top in unix and try to see if some sequence of steps takes a chunk of memory. You can also watch it and see if it happens regularly like every 10 seconds or whatnot.

    If the leak is large, look for large allocations (maybe do a search on every new statement in the program and look for new blah[100000] or the like). If it is small, you can try the reverse and look for a function that is called a lot, maybe a function that returns a pointer as these can be written such that the caller must deallocate...

    If all else fails you can try to match every new to a delete statement, with a script or the like, and at least eliminate some of the possibilities and highlight things to check. If the programmers play swicharoo with pointers everywhere (x = y, ... delete y, x is also deleted sort of games) then this will not work. If most of the code is sane, it will.

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    BoundsCcheker and many other memory diagnostics tools often have false alarms, i.e., they report memory leaks that aren't really leaks. The best thing to do is reduce the amount of manual memory allocation in your programs and use standard containers instead. It's a very general piece of advice. If you can provide more details about the nature of your application, which memory management policy it uses etc., I'll be able to give more pinpointed guidelines. The bottom line is this: better design is tremendously more helpful than using memory leak detectors.
    Danny Kalev

  4. #4
    Join Date
    Dec 2005
    Posts
    134
    I have learned that you need to have a
    Code:
    CoUnitialize()
    to go along with every
    Code:
    CoInitailze(0)
    I wonder if I need something to go along with my
    Code:
    CoCreateInstance
    calls.

  5. #5
    Join Date
    Dec 2003
    Posts
    3,366
    possibly -- you will have to look at the documentation or if you have none, try it in a stub program and see if that is a leak?

  6. #6
    Join Date
    May 2006
    Posts
    176
    Quote Originally Posted by Complete
    . . .
    I wonder if I need something to go along with my
    Code:
    CoCreateInstance
    calls.
    After you do not more need the object created with CoCreateInstance, I think you have to free it by calling Release function:

    Code:
    SomeObject * object;
    CoCreateInstance(. . ., &object);
    . . .
    object->Release();
    I hope this makes sense.

  7. #7
    Join Date
    May 2007
    Posts
    843
    Every new should has one delete.

    Be aware of transfer ownership to another during passing arguments.

    I hope this help.

Similar Threads

  1. Possible Memory Leak in Java application
    By esi-eric in forum Java
    Replies: 3
    Last Post: 02-24-2005, 06:58 PM
  2. Advice for breaking into the field (repost)
    By David K. in forum Careers
    Replies: 0
    Last Post: 01-15-2001, 09:21 PM
  3. Interface leaks in C#?
    By Kevin Burton in forum .NET
    Replies: 12
    Last Post: 10-09-2000, 10:29 AM
  4. Advice for breaking into the field
    By David K. in forum Careers
    Replies: 0
    Last Post: 10-06-2000, 05:29 PM
  5. Re: MS Provider For Oracle : Memory Leak ?
    By John Grandy in forum VB Classic
    Replies: 0
    Last Post: 04-08-2000, 08:35 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links