[comp.os.msdos.programmer] Borland C++'s "heapcheck" function

sorrow@oak.circa.ufl.edu (05/30/91)

I have been looking through my new Borland C++ stuff and ran into the functions
that start with "heap" such as heapcheck(), heapcheckfree(), heapfillfree(),
heapwalk(), etc.

Anyone have experience with them?  The documentation is shoddy.  It seems to
imply that they can be used to track errant bugs, and the guy I talked to at
Borland seemed pretty confused ("Well, um, yeah, I guess it could be used for
finding bad pointers".  "Then what else could I use it for?"  "Oh, to see if
you've overwritten memory" )


If you have any ideas, I'd like to know what they do.

Brian
/*
Brian Hook -- MS-DOS Programmer for Contract
-----------------------------------------------------------------
"I was in the kitchen, Seamus, that's my dog, was outside....and buried
ALIVE....fritter and waste...but this one goes to 11!....anymore of that
plutonium nyborg?....SNOW TIME!....This is home...this is Mean Street..
*/

35004_240@uwovax.uwo.ca (Charles McClellan) (05/30/91)

I have used all of these functions in a memory checking package. 
The header for this package redefines malloc, free, etc to my own
versions.  Then I call heapcheck, and heapcheckfree before allocating
any memory and heapcheckfill after freeing any memory.

These functions will basically catch most cases where you continue
to use a pointer after freeing it.  Heapfillfree and heapcheckfree will also 
catch memory overwrites of the heap.

I also allocate an extra word before and after each block in which I
store the size of the block. This lets me catch most overwrites of
the block.

I have found these functions quite useful.

-- 
Charles McClellan:	SLIS
			Elborn College
			University of Western Ontario
			London, Ont.  Canada
			Tel:  519-661-3542  ext. 8485
			C.McClellan@uwovax.uwo.ca

minar@reed.edu (05/31/91)

In article <1991May30.090835.9320@uwovax.uwo.ca> 35004_240@uwovax.uwo.ca (Charles McClellan) writes:

>These functions will basically catch most cases where you continue
>to use a pointer after freeing it.  Heapfillfree and heapcheckfree will also 
>catch memory overwrites of the heap.

how does it do that? What does it do, exactly?

35004_240@uwovax.uwo.ca (Charles McClellan) (06/01/91)

In article <m0jizjG-0001OzC@dharma.reed.edu>, minar@reed.edu writes:
> In article <1991May30.090835.9320@uwovax.uwo.ca> 35004_240@uwovax.uwo.ca (Charles McClellan) writes:
> 
>>These functions will basically catch most cases where you continue
>>to use a pointer after freeing it.  Heapfillfree and heapcheckfree will also 
>>catch memory overwrites of the heap.
> 
> how does it do that? What does it do, exactly?

To the best of my knowledge the way heapcheck() works is this:

the malloc functions keep a linked list of free and allocated blocks. The 
header for each block contains among other things the size of the block
and the pointer to the next block. Heapcheck() walks this list checking that 
each header contains reasonable values.  

Heapfillfree() enables you to fill all free blocks on the list with 
an arbritrary value.  Heapcheckfree() then checks to see if all free values
still have this value.  Thus if you continue to use a pointer after
freeing it, the fill value will probably be changed.  The fill value
can also be changed if you have a wild pointer that is writing over
parts of the heap.

For these functions to be useful, you must call them before and after
every use of malloc() and free().  This is easiest if you provide
your own replacements for them.  This was discussed in a couple of
articles in the August 1990 Dr. Dobbs.  And I have since seen a commercial
library advertised that replaces calls to malloc() and free() with
error checking versions. 

By themselves Heapcheck() and company are not that useful, but when
used in error checking versions of malloc() etc, I have found them
to be very useful.


-- 
Charles McClellan:	SLIS
			Elborn College
			University of Western Ontario
			London, Ont.  Canada
			Tel:  519-661-3542  ext. 8485
			C.McClellan@uwovax.uwo.ca