JAMES S HAINES wrote:
> I am trying to build a function that will return the contents of a field
> (basic). The trouble is that all the examples I have found are void
> functions that do not return anything.
>
> Question #1: when I unlock the handle "h", does that invalidate the pointer
> to the text string so that it cannot be reliably returned?
yes
> Question 2: If that is the case, how do I work around it?
method #1:
allocate memory memory with MemPtrNew(), copy the string there,
return it to the caller, and require the caller to free it when
they're done with it.
method #2:
return the handle and let the caller worry about locking and
unlocking it. but make sure the caller doesn't leave it locked
too long so that the OS can't replace replace it with a new
handle if necessary when the user enters new text in the field.
method #3:
let the caller supply a buffer (with a size!) and write into that
method #4:
create a small C++ class with a function that returns the
pointer from the locked handle and unlocks the handle in
its destructor. same caveat as in #2 about leaving the
handle locked too long; this means the client of the class
needs to make sure the object's lifetime isn't too long
(bad design since that's not obvious to the client).
method #5:
create a small C++ class that copies the field's data into
dynamic memory it owns, has an accessor function to let you
get a pointer to that copy, and which frees the dynamic
memory when the object gets destroyed. Then you can make
instances of this class as local variables, call GetString()
or whatever, and it will free itself when it goes out of scope.
method #6:
write your program so that you don't need such a function. :-)
Hope that helps.
- Logan
--
For information on using the Palm Developer Forums, or to unsubscribe, please
see http://www.palmos.com/dev/support/forums/