[futurebasic] Re: Handles

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : May 1998 : Group Archive : Group : All Groups

From: Rick Brown <rbrown@...>
Date: Thu, 28 May 1998 20:11:56 -0500
Forrest wrote:

> Do you have to get rid of all the handles that your program creates? or
> will they be purged when the memory is needed? Well actually, how does that
> all work, because I'm almost finished with my project and now I have to get
> rid off all the memory leaks.

They won't _necessarily_ be purged when memory is needed--there are
handles which are purgeable, and handles which are unpurgeable.  A
handle that you create using the NEWHANDLE function is not purgeable.  A
handle that you create by reading a resource from disk (using something
like FN GETRESOURCE) is purgeable _if_ the resource itself has been
marked as purgeable (you can use ResEdit to mark a resource as
purgeable, if you want).

Although some people would disagree with me, I do _not_ recommend using
purgeability as a method of "garbage collection" for unused handles.  It
wasn't designed for that--it was designed to let the Memory Manager
temporarily "swap out" a block, under the assumption that you are _not_
finished using the block's handle.  The Memory Manager _keeps_ the
handle around, even after the block is purged, and this can lead to
unexpected results if you're not careful.  For example, suppose you do
this:

   h& = FN GETRESOURCE(_"TEXT", 128)

Normally, this loads a resource into memory.  However, if that resource
was loaded into memory previously, but has since been purged, then the
above statement does _not_ reload the resource into memory!  It will
return a "valid," nonzero handle, but the handle won't refer to any
memory block.

IMHO, you should explicitly dispose of any handle your program creates,
when you're completely through with it.  Use FN DISPOSHANDLE (or DEF
DISPOSEH) if it's a non-resource handle; use CALL RELEASERESOURCE if
it's a resource handle.  In some cases, your program creates handles
indirectly; for example, at least a couple handles are created every
time you open an edit field.  In those cases, there's usually some kind
of "CLOSE" statement (like EDIT FIELD CLOSE) which takes care of
disposing the handles for you.  When a handle "belongs" to an existing
object like an edit field, you should _never_ try to dispose of the
handle while the "object" is still open.

I know this is a complex topic: I'm working on a "Pointers & Handles"
tutorial, soon to appear on my web site, which hopefully will create
some sense out of all this.

- Rick