Double free and memory leak

H

Harish

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
 
M

Mike Jolley

Harish said:
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.
 
D

David White

Harish said:
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
 
C

CBFalconer

Mike said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top