PDA

View Full Version : When will delete fail?



Pyabo
09-29-2004, 04:40 PM
I've got an app that is consistently leaking memory... the odd thing is that the code appears to be correct. The code is clearly calling "delete []" in the proper context... but the memory is NOT being freed. What can cause this? Some very specific heap correuption is the only thing I can think of, but the code looks correct and I'm not seeing any other telltale signs of memory corruption. I've also double-checked that delete is not being overloaded.

Further details:


Basically, it's a loop where something is being downloaded... looks like this:

download some stuff into OLDBUFF
if (we still got some more to download) then
1. allocate a new bigger buffer NEWBUFF
2. copy OLDBUFF into NEWBUFF
3. delete OLDBUFF (should release mem at this point!?!)
4. OLDBUFF pointer = NEWBUFF

Repeat ad nauseum... The problem appears that the delete call is not actually freeing the memory. :confused:

Am I missing something totally obvious here?

Rainer Deyke
09-29-2004, 04:58 PM
Without seeing the actual code, I can only guess. Here are some guesses:

1. Some memory managers keep freed memory around instead of returning it to the operating system. This memeory is then used to fulfill the next allocation request. Could this be happening?

2. A broken overload of operator delete[] might be called (only if you overloaded operator delete[]).

3. Can you confirm that the code actually reaches the point where delete[] is called?

4. Can you confirm that no other memory is being allocated?

mahlzeit
09-29-2004, 11:43 PM
I don't know if this applies in your case, but if you delete a void pointer, the destructors of subclasses won't be called. So the following would leak memory:


class Buffer
{
Buffer() { allocate memory }
virtual ~Buffer() { free memory }
}

void* buffer = new Buffer;
delete buffer;

Carrot
09-30-2004, 04:12 AM
Also, are you deleting objects through their base class pointers? Make sure you have virtual destructors in this case.

Bluecat
09-30-2004, 04:45 AM
Aaack! Using void pointers in C++! Bad programmmer, no twinkie! ;)

nalenb
09-30-2004, 05:52 AM
Paste in the loop code with just the allocations and deletes. Are you deleting oldbuf once it's not used anymore?

Nikster
09-30-2004, 07:44 AM
Might be worth turning all exception debuging on (assuming your using visstudio) as if an exception occurs in the dtors and you don't catch any exceptions it could leak as it won't have processed the dtor properly.

--

Just re-reading, you mention oldbuf, newbuf, are you sure you wan't to be calling delete[] and not just delete ? I can't see how you actually 'new' the stuff.