Discussion:
Problem with HeapFree
(too old to reply)
Varadha
2004-02-06 16:23:43 UTC
Permalink
HI,
I am getting the following message in the debug window when i run
my application.
HEAP[App.Exe]: HEAP: Free Heap block d6f1a78 modified at d6f1f1c after
it was freed

I also found that in my application heap allocation is done in the
same memeory location twice, but before the second allcaotion is done
on the same memory location first allocation is freed. Does this
causes any memory problem.

i have given below the sequence of events that place. u can notice
that all the memory address(MemAdd) are same.

HeapAlloc Size=80013 MemAdd=225453672
HeapFree MemAdd=225453672
HeapAlloc Size=1182 MemAdd=225453672
HeapFree MemAdd=225453672


Thanks in Advance,
Varadha
Stephen Kellett
2004-02-06 16:37:28 UTC
Permalink
Post by Varadha
I am getting the following message in the debug window when i run
my application.
HEAP[App.Exe]: HEAP: Free Heap block d6f1a78 modified at d6f1f1c after
it was freed
This means that somewhere in your application you have a pointer to this
memory and you are writing data to it (or before it or after it). A good
strategy for pointers is to NULL them prior to use, and NULL them after
freeing any memory they point to. Then before you use them check if they
are NULL or not.

BYTE *ptr;

ptr = (BYTE *)HeapAlloc(hHeap, 0, size);
if (ptr != NULL)
{
// do whatever work...

calculateTonightsWinningLotteryNumbersIWish();

// tidy up

HeapFree(hHeap, 0, ptr);

// now if you try to access the memory it may work
// (corrupting the memory) or it may crash. Who knows?
// this is probably what your program is doing - using a
// pointer to memory that has already been freed

*ptr =0x55; // memory corruption (or possible crash)

// reset the pointer

ptr = NULL;

// now if you try to write to pointer rather than
// corrupting memory, you'll get an access violation
// trying to write to 0x00000000

*ptr = 0x55; // crash!
}

a contrived example - that makes little sense. But if you are storing
the pointers for later use, these concepts help a lot.

Stephen
--
Stephen Kellett
Object Media Limited http://www.objmedia.demon.co.uk
RSI Information: http://www.objmedia.demon.co.uk/rsi.html
Varadha
2004-02-07 04:53:16 UTC
Permalink
Post by Stephen Kellett
This means that somewhere in your application you have a pointer to this
memory and you are writing data to it (or before it or after it). A good
strategy for pointers is to NULL them prior to use, and NULL them after
freeing any memory they point to. Then before you use them check if they
are NULL or not.
can u say me a tool which i can use to detect the problem since the
project that i work is huge and it is difficult to find it by
debugguing.

Is the follwing information dosent cause any memory problem..
Post by Stephen Kellett
I also found that in my application heap allocation is done in the
same memeory location twice, but before the second allcaotion is done
on the same memory location first allocation is freed. Does this
causes any memory problem.
i have given below the sequence of events that place. u can notice
that all the memory address(MemAdd) are same.
HeapAlloc Size=80013 MemAdd=225453672
HeapFree MemAdd=225453672
HeapAlloc Size=1182 MemAdd=225453672
HeapFree MemAdd=225453672
Stephen Kellett
2004-02-07 11:44:25 UTC
Permalink
Post by Varadha
Post by Stephen Kellett
This means that somewhere in your application you have a pointer to this
memory and you are writing data to it (or before it or after it). A good
strategy for pointers is to NULL them prior to use, and NULL them after
freeing any memory they point to. Then before you use them check if they
are NULL or not.
can u say me a tool which i can use to detect the problem since the
project that i work is huge and it is difficult to find it by
debugguing.
Even more reason to NULL the pointers as I have recommended. In 1994 I
was hired by a large telecomms provider to find and fix a bug in an
application their own staff had been unable to find in 3 months of
debugging. I found the bug by the end of the first day and had it fixed
by lunchtime the second day. Principal reason for this speed was
ensuring that all freed pointers were set to NULL, this allowed for
trivial detection of the reuse of dead pointers, which were otherwise
corrupting memory. This was in a 10,000 line application.

Most memory debugging tools provide support for this type of problem, to
a greater or lesser extent, depending on the tool and the approach the
tool takes. Regardless of the tool you choose, I would strongly
recommend setting pointers to NULL after you free/delete/HeapFree()
them. This will allow you to identify problems with much less
stress/time/effort and less tool usage.

The tool I use is Memory Validator. To detect memory corruption, you
will need to use the advanced features of the software - so read the
help file and the tutorial.

http://www.softwareverify.com

There is a 30 day download and a tutorial.

Stephen
--
Stephen Kellett
Object Media Limited http://www.objmedia.demon.co.uk
RSI Information: http://www.objmedia.demon.co.uk/rsi.html
Varadha
2004-02-09 04:43:19 UTC
Permalink
I am allocationg memory using the HeapAlloc function. If i call
HeapDestroy before freeing the heap created using HeapFree function,
can any error may arise due to this.

i.e
hHeap = HeapCreate(0, 1000000, 0);
pV = HeapAlloc(hHeap, HEAP_ZERO_MEMORY , ulSize);
HeapDestroy(hHeap);

here before freeing pV, we are Destroying the Heap. Is this causes any
problem.
Stephen Kellett
2004-02-09 11:31:05 UTC
Permalink
Post by Varadha
I am allocationg memory using the HeapAlloc function. If i call
HeapDestroy before freeing the heap created using HeapFree function,
can any error may arise due to this.
i.e
hHeap = HeapCreate(0, 1000000, 0);
pV = HeapAlloc(hHeap, HEAP_ZERO_MEMORY , ulSize);
HeapDestroy(hHeap);
here before freeing pV, we are Destroying the Heap. Is this causes any
problem.
No problem, although it may lead any memory leak tracking tool to become
confused as to the state of "pV". It may think "pV" is leaked when in
fact it is not.

This is perfectly valid tactic and will allow you to discard many
allocations at once. Very useful for a specialized allocator.

Stephen
--
Stephen Kellett
Object Media Limited http://www.objmedia.demon.co.uk
RSI Information: http://www.objmedia.demon.co.uk/rsi.html
unknown
2004-02-10 23:40:00 UTC
Permalink
Post by Varadha
I am allocationg memory using the HeapAlloc function. If i call
HeapDestroy before freeing the heap created using HeapFree function,
can any error may arise due to this.
i.e
hHeap = HeapCreate(0, 1000000, 0);
pV = HeapAlloc(hHeap, HEAP_ZERO_MEMORY , ulSize);
HeapDestroy(hHeap);
here before freeing pV, we are Destroying the Heap. Is this causes any
problem.
No problem, as long as you don't try to free pV after you destroy
the heap.

Destroying a heap is enough to free all allocations from it.
Of course, this doesn't do any cleanup besides releasing the
memory, so if you were using HeapAlloc as a custom C++
allocator you would have problems because destructors for
your objects would not be run, etc.

Continue reading on narkive:
Search results for 'Problem with HeapFree' (Questions and Answers)
4
replies
svchost.exe using up all my CPU on Windows XP?
started 2012-03-19 23:25:39 UTC
software
Loading...