> From: Richard M. Hartman [mailto:[EMAIL PROTECTED]]
> What does the OS normally do for memory management
> on label objects on forms?  Likewise, what must I do if
> I am going to mess w/ the label text?

When a form is initially loaded, the text pointer in every control (and
label, etc) points to a string of bytes that is embedded inside the form
object itself.  The entire form object itself is a single chunk in the
dynamic heap.

When you close a form, the OS basically just frees the single chunk that
constitutes the form object.  It does not attempt to free any other memory
chunks that may be referenced by the various pointers in controls and labels
within the form.  It just assumes that nothing got changed, so that the
controls and labels still point to the embedded strings within the form
object.

Given these facts, the memory management rules should be easy to figure out.
If you change text by a _copy_ operation (FrmCopyLabel, FrmCopyTitle, or
overwrite a control's label area with StrCopy) then the object's pointer
doesn't change and still points to the same buffer inside the form object.
The form object still "owns" that memory.  So closing the form will work
just as before.  Everything goes away.

But if you change text by a _set_ operation (CtlSetLabel, FrmSetTitle, or
manually poking a label structure) then the object's text pointer now points
somewhere outside the form object itself.  _You_ own that memory.  The way
you clean it up depends on where that memory lives and its lifetime.  If you
allocated it in the heap, then you should free it.  If it's a pointer to a
static character string in your code, then there is nothing to clean up.  If
it's a pointer to a locked resource of some sort, then you should unlock and
release that resource.

And if you do _two_ set operations on a given object, then remember that you
own the first string that is getting replaced and you do need to free it in
an appropriate way.

The key to all this is to understand exactly who (you -or- the form object)
owns the memory you're about to fiddle with.  If you own it, then you need
to clean it up.

-slj-


-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palm.com/devzone/mailinglists.html

Reply via email to