Double free and memory leak

Discussion in 'C Programming' started by Harish, Apr 1, 2005.

  1. Harish

    Harish Guest

    Hi,

    I am newbie to this field, So can anyone clear my doubt. I have an
    application that leaks memory recursively, but I don't find any cause
    for that. I have checked for allocation and deallocation of every
    piece of memory. All I can find is a double free on a buffer, which is
    in an infinite loop.
    Can a Double free bug cause memory leaks? My thinking is based on the
    implementation of malloc/free, this causes memory corruption and in a
    rare case the freed buffer may be reallocated in such a way that the
    second free causes only a part of the buffer to be freed, or making it
    impossible to free some other buffer. Does anyone agree with me?

    Thanks in advance,
    regards,
    harish
     
    Harish, Apr 1, 2005
    #1
    1. Advertising

  2. Harish

    Mike Jolley Guest

    "Harish" <> wrote in message
    news:...
    > Can a Double free bug cause memory leaks? My thinking is based on the
    > implementation of malloc/free, this causes memory corruption and in a
    > rare case the freed buffer may be reallocated in such a way that the
    > second free causes only a part of the buffer to be freed, or making it
    > impossible to free some other buffer. Does anyone agree with me?


    In my experience, freeing a pointer that's already been freed causes
    heap corruption. This could result in any or all of the symptoms
    you mentioned. Even if it's possible for an implementation to detect
    a double-delete, maybe the second delete happens long after the
    first, i.e. if another thread interrupted my thread in betwen.
     
    Mike Jolley, Apr 1, 2005
    #2
    1. Advertising

  3. Harish

    David White Guest

    "Harish" <> wrote in message
    news:...
    > Hi,
    >
    > I am newbie to this field, So can anyone clear my doubt. I have an
    > application that leaks memory recursively, but I don't find any cause
    > for that. I have checked for allocation and deallocation of every
    > piece of memory. All I can find is a double free on a buffer, which is
    > in an infinite loop.
    > Can a Double free bug cause memory leaks?


    Anything can happen.

    > My thinking is based on the
    > implementation of malloc/free, this causes memory corruption and in a
    > rare case the freed buffer may be reallocated in such a way that the
    > second free causes only a part of the buffer to be freed, or making it
    > impossible to free some other buffer. Does anyone agree with me?


    Unless nothing has happened in the meantime and the allocator detects that
    the block is already free or harmlessly marks an already-free block free
    again, it will most likely be completely screwed up and you couldn't predict
    the effect.

    DW
     
    David White, Apr 1, 2005
    #3
  4. Harish

    CBFalconer Guest

    Mike Jolley wrote:
    > "Harish" <> wrote in message
    >
    >> Can a Double free bug cause memory leaks? My thinking is based on
    >> the implementation of malloc/free, this causes memory corruption
    >> and in a rare case the freed buffer may be reallocated in such a
    >> way that the second free causes only a part of the buffer to be
    >> freed, or making it impossible to free some other buffer. Does
    >> anyone agree with me?

    >
    > In my experience, freeing a pointer that's already been freed
    > causes heap corruption. This could result in any or all of the
    > symptoms you mentioned. Even if it's possible for an
    > implementation to detect a double-delete, maybe the second
    > delete happens long after the first, i.e. if another thread
    > interrupted my thread in betwen.


    Any use of a pointer that has been freed leads to undefined
    behaviour. The safe course is usually to set it to NULL
    immediately after freeing. Of course, this doesn't handle copies
    that may be lying around.

    F'ups set to c.l.c. This is a C question, not C++, and virtually
    nothing should be cross-posted between the groups.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
     
    CBFalconer, Apr 1, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,567
    Victor Bazarov
    Feb 17, 2005
  2. Harish

    Double free and memory leak

    Harish, Apr 1, 2005, in forum: C++
    Replies:
    3
    Views:
    6,599
    CBFalconer
    Apr 1, 2005
  3. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    721
    Eric Sosman
    Mar 22, 2005
  4. Panduranga Chary

    How memory function free() knows how much memory to free.

    Panduranga Chary, Dec 27, 2007, in forum: C Programming
    Replies:
    2
    Views:
    429
    Keith Thompson
    Dec 27, 2007
  5. mohammed_a_o
    Replies:
    0
    Views:
    306
    mohammed_a_o
    Nov 30, 2010
Loading...

Share This Page