Help! My app is crashing when calling HeapFree, but only when running
under WIndows NT.
I am using HeapAlloc to grab a chunk of memory, and HeapFree to release it
later on. The two routines look like this:
To declare a chunk of memory:
chunk := HeapAlloc(GetProcessHeap, HEAP_ZERO_MEMORY, siz);
To release that chunk of memory:
if chunk <> nil then
begin
b := HeapFree(GetProcessHeap, 0, chunk); // "b" is a BOOL
chunk := NIL;
end;
This works fine under Windows 98!! But under Windows NT, it will work for
a while but eventually it will crash when it tries to execute the
HeapFree, giving an Access Violation error.
-- anything obviously wrong here?
-- that line "chunk := NIL" should be ok, yes?
-- I am doing this within a DLL being called by a thread. Any concerns?
-- would it help if I switched to a different set of memory routines?
(e.g., GlobalAlloc, VirtualAlloc)
Thank you!!!
Kevin Killion
ke...@shsmedia.com
There's nothing wrong with that. It's most probably a double free: NT
and 98 just have different memory management algorithms, so NT happens
to deallocate the page before the second free, and 98 doesn't.
> -- that line "chunk := NIL" should be ok, yes?
It's a good idea, to avoid using the free block. But if there's a
pointer to it anywhere else, this still leaves scope for mistakes.
> -- would it help if I switched to a different set of memory routines?
> (e.g., GlobalAlloc, VirtualAlloc)
It might just hide the problem. The easiest solution would be to use
a memory debugging tool. I'm not sure what's available for Pascal,
but some would work if you switched to use malloc/free (yes, even in
Pascal), and some of them can be taught about new allocation
interfaces. If you don't have such a tool, try clearing the block
before you free it, that might catch any use (assuming you have a
decent amount of assertions in your program) - then go buy the tool
anyway, it'll save you a lot of time in the future. Start looking at
<http://www.cs.colorado.edu/%7Ezorn/MallocDebug.html>.
--
Pekka P. Pirinen, Adaptive Memory Management Group, Harlequin Limited
A programming language is low level when its programs require attention to
the irrelevant. - Alan Perlis