Hello Tony,

A few notes about your code:
1. You shouldn't need to perform the MemHandleNew - the field already
has memory allocated
2. You should check to see if your handle is valid before proceeding.
You should also check to see if the pointer is valid. The usual idiom
is like this:
memH = FldGetTextHandle(fieldP);
if (memH)
{
   memP = MemHandleLock(memH);
   if (memP)
   {
      // Operations using memP
   }
   MemHandleUnlock(memH);
}
3. I don't think you can rely on the pointer "p" to be valid after
returning from your function. You have unlocked the memory on the line
before the return, so the memory is now relocatable. It may stay in
place, it may not. You have a number of options for returning the
data:
a) Return the memory handle - this way, the user of the function can
just perform a MemHandleLock to get a pointer to use.
b) Take a pointer and max length as arguments to the function, and use
StrNCopy to copy to that pointer.
c) Allocate new memory with MemPtrNew, and return the pointer. Note
that you will somehow have to manage freeing this pointer at some
time.
4. You shouldn't need to resize the memory handle to read the field's
text - it already has enough memory to hold the text (otherwise where
is it stored?)

Hope that helps,
Adrien.

Monday, November 15, 2004, 10:23:53 AM, you wrote:

TJ> I use the following code to retrieve text from a field...

TJ> 
---------------------------------------------------------------------------------------------------------------------------------------------------------------
TJ>     h = MemHandleNew(50);           // allocate chunk of memory in
TJ> heap
TJ>     FieldPtr = FrmGetObjectPtr(pForm, FrmGetObjectIndex(pForm,
TJ> controlID));
TJ>     h = FldGetTextHandle(FieldPtr);
TJ>     p = MemHandleLock(h);           // prevent the chunk from moving
TJ> (lock) and obtain pointer to location
TJ>     MemHandleResize(h, StrLen(p)+1);
TJ>     MemHandleUnlock(h);
TJ>     return p;
TJ> 
---------------------------------------------------------------------------------------------------------------------------------------------------------------
TJ> my question is how to check that the field is not empty.  If the field
TJ> is empty, I get a NULL handle error at the designated line.


TJ> thanks,

TJ> ~Tony



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

Reply via email to