MemHandleLock "locks down" a moveable chunk of memory.  For every lock, you
should unlock it to allow the memory manager to handle the moveable chunk of
memory.  (MemHandleUnlock)

Using MemHandleFree, allows this piece of memory to be written over.  For
example, in a common function used by many people...

FieldPtr SetFieldTextFromHandle(Word fldID, Handle txtH)
{
     Handle oldTxtH;
     FormPtr frm;
     FieldPtr fld;

     // Get the Active Form
     frm = FrmGetActiveForm();

     // Get the Pointer to the Field
     fld = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, fldID));

     // Error?!
     ErrNonFatalDisplayIf(!fld, "Incorrect Field Specified");

     // Swap handles!
     oldTxtH = FldGetTextHandle(fld);
     FldSetTextHandle(fld, txtH);

     // Clear field display
     FldEraseField(fld);

     // Draw field display
     FldDrawField(fld);

     // Free some memory baby!
     if(oldTxtH)
          MemHandleFree(oldTxtH);

     // Return the field
     return(fld);
}

Note that to replace the string present in the field, we grab the existing
handle and set a new handle to the field.  Then the old handle is freed
forever.

I would highly recommend that you don't use any handle after it's been
freed.  Just because the data seems to be there when you access afterwards,
it doesn't mean it won't be written over, and lead to bugs in the future.

Regards,

--

Tim Astle



-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to