Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Memory leak prevention methodogies

  1. #1
    c___newbie is offline Newbie
    Join Date
    Nov 2007
    Posts
    1
    Rep Power
    0

    Memory leak prevention methodogies

    Hi all,
    I’m studying a problem releated to memory leakage.
    Now, there are many tools to check memory leakake at run-time (dynamic analysis), and some tools can check memory leakage at compile-time (static analysis). But I have some questions:
    How do you think to prevent memory leakage from coding phase (without using tool)? (or In coding phase, what do you have to do to prevent memory leakage?)
    I want to investigate from many programmers in many countries.
    Can you help me?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143
    The basic technique is to make sure that for every malloc() there is a corresponding free(). This takes care of the most obvious problem, but is far from error proof. For example, if you have code like the following:
    Code:
    int * ptr;
    for (i=1;i<10;i++){ptr=malloc(int);}
    free(ptr);
    will leak memory like crazy because the malloc is inside the loop, but the free is outside the loop.
    This type of situation can happen easily when dealing with data structures such as linked lists, where you'll have a pointer to the head of the list and the current node of the list, and each node has a pointer to the next node. When "deleting" the linked list you have to be very careful to free each node without losing access to the list in the process.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Oct 2007
    Posts
    538
    Rep Power
    21
    I get lazy and use garbage collection. Let someone else worry about memory management .

    As I see it. Unless I need programs that are always instantly responsive (i.e. 10ms pauses are unacceptable) then there is little or no reason not to use conservative GC. It does not produce an overly undue overhead in terms of processor time (because the majority of the deallocation code is only called once per thousands of deallocations while with manual memory management it's called once per deallocation). The only real issue is that GC can cause intense spots of activity where the program locks up. This is not usually noticeable in most applications.

    A lot of C++ programmers use smart pointers to implement reference counting GC. This can work at a near manual level of efficiency. The only issue is circular references but it does generally stabilise memory usage. For me that is the key, I want to be able to tell people that this application uses N MiB of memory. I don't care that it wastes O MiB as long as it wastes O statically and no more than O.

  5. #4
    Reji is offline Newbie
    Join Date
    May 2009
    Posts
    12
    Rep Power
    0

    Re: Memory leak prevention methodogies

    Can you, please, tell me some tools to check memory leakake at run-time?

  6. #5
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Memory leak prevention methodogies

    There are a variety of tools, depending on your OS, compiler, etc. The best tool will depend on the details of your environment.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #6
    Reji is offline Newbie
    Join Date
    May 2009
    Posts
    12
    Rep Power
    0

    Re: Memory leak prevention methodogies

    OS-WinXP
    environment- Microsoft Visual Studio 6.0
    I was advised to use Deleaker. Is it really good?

  8. #7
    Sysop_fb is offline Programmer
    Join Date
    Apr 2009
    Location
    Missouri
    Posts
    150
    Blog Entries
    2
    Rep Power
    12

    Re: Memory leak prevention methodogies

    Maintain control of your memory region pointers.
    Never use your pointer for the return value of realloc because realloc isn't required to succeed, it can fail and return NULL and you can lose track of your original block of allocated memory so.. use a temporary pointer.

  9. #8
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Memory leak prevention methodogies

    VS6 isn't very good. I haven't used Deleaker.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  10. #9
    Reji is offline Newbie
    Join Date
    May 2009
    Posts
    12
    Rep Power
    0

    Re: Memory leak prevention methodogies

    Thanks

  11. #10
    roboticforest is offline Programmer
    Join Date
    Dec 2008
    Location
    Portland OR
    Posts
    111
    Rep Power
    0

    Re: Memory leak prevention methodogies

    One thing you can look into (and I mean carefully research before trying) is overloading the global new and delete operators.

    Doing this will allow you (if nothing else) to keep a running total of all allocations, and deallocations made during your program's run time. If you have more allocations than deallocations, then you have a leak somewhere.

    This setup can get far more advanced, but there's a start.
    Dave

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Memory Leak (?)
    By Bat0u89 in forum C and C++
    Replies: 16
    Last Post: 01-13-2011, 10:28 AM
  2. Memory Leak
    By espdev-darkness in forum C and C++
    Replies: 8
    Last Post: 11-29-2010, 04:31 PM
  3. Hack prevention?
    By RobotGymnast in forum C and C++
    Replies: 5
    Last Post: 08-14-2009, 06:56 PM
  4. Detecting memory leak in complicating source code
    By mosxopul in forum C and C++
    Replies: 1
    Last Post: 03-30-2009, 10:13 AM

Tags for this Thread

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