> FormPtr hiddenfrmP;
> FieldPtr fieldP;
> MemHandle textH;
> Char* str;
>
> // initialize "hidden" form and retrieve "hidden" field
> hiddenfrmP = FrmInitForm (HiddenForm);
> FrmDrawForm(hiddenfrmP);
> fieldP = (FieldPtr)FrmGetObjectPtr(hiddenfrmP,
> FrmGetObjectIndex(hiddenfrmP, HiddenHiddenField));
> textH = FldGetTextHandle(fieldP);
Since you're going to overwrite textH below, and eventually give the field a
different handle, you need to free any memory that the field is already
using.
textH = FldGetTextHandle(fieldP);
FldSetTextHandle(hiddenfrmP, NULL);
if (textH != 0)
MemHandleFree(TextH);
> textH = MemHandleNew ( 5 );
> str = MemHandleLock(textH);
> StrCopy(str, "Test");
> MemHandleUnlock(textH);
> FldSetTextHandle(fieldP, textH);
> FldDrawField(fieldP);
At this point, you've put the word "Test" into the field, and it is using
the memory handle identified by textH.
> MemHandleFree(textH);
You cannot free the handle that you've given to the field. Once you've
given the handle to the field, leave it alone. The field will free the
handle when it is removed.
When you want to change the handle that the field is using, then you are
responsible for freeing the memory handle that was used before. Do this by
getting the handle, setting the handle to NULL (or a different handle), then
freeing the old handle. Don't free it while the field is using it.
>
> // perform lookup
> textH = FldGetTextHandle(fieldP);
Same comment as above about releasing the handle in use by the field.
Is your goal here to put an empty buffer of 161 bytes into fieldP? If so,
try this:
oldH = FldGetTextHandle(fieldP);
textH = MemHandleNew(161);
FldSetTextHandle(fieldP, textH);
if (oldH != 0)
{
MemHandleFree(TextH);
oldH = 0;
}
> textH = MemHandleNew ( 161 );
>
> PhoneNumberLookup(fieldP);
>
> FldSetTextHandle(fieldP, textH);
The above Set is what could orphan memory. If fieldP already has text in
it, then that block of memory will be "lost" when you give the field a new
memory handle. This is why you need to get the memory handle that the field
is using BEFORE you change the handle, then free it AFTER you've given the
field a new handle.
> FldDrawField(fieldP);
>
> // free handle to prevent memory leak
> StrIToA(result1P, FldGetTextLength(fieldP));
> MemHandleFree(textH);
This MemHandleFree won't work (or worse, will work and crash) because the
field is using the handle. Again, don't free it.
>
> FrmDeleteForm(hiddenfrmP);
>
> Thanks,
> Dave
>
Send me an e-mail offline if you need more explanation about fields. You
might find a book that discusses fields helpful. The "Palm OS Programming
for Dummies" book is dated, but does include detailed information on
fields, as well as examples.
Jeff
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/