> From: Ingo Claro Fox
>
> I'm freeing the memory in the correct way or it will last some
> becouse of the static??
> are there some other aproches?? (if I remove static I loose the
> pinter and see garbage if I press a hardkey.
>
> static Char *buf;
> [snip]
> case frmOpenEvent:
> buf=MemPtrNew(10);
> [...]
> StrIToA(buf,4);
> CtlSetLabel(btnP,buf); // set label to button
> handled=true;
> break;
>
> case keyDownEvent:
> [...]
> StrIToA(buf,5);
> CtlSetLabel(btnP,buf); // set label to button
> handled=true;
> break;
> case frmCloseEvent:
> MemPtrFree(buf);
> break;
In general, if you do MemPtrNew, you should do MemPtrFree at some later
point. The key issue in this code is that the memory containing the string
you use for a control label must be accessible for the life of the control.
If you declare buf static, the pointer doesn't get destroyed when your app
exits the function. If you don't declare buf static, the memory for the
string is still there (MemPtrNew), but the pointer won't be valid when your
function is entered later (for the keyDownEvent, and again later for the
frmCloseEvent). Freeing the memory when the form is closed is OK.
Another approach is to declare a global variable like "Char labelBuf[10]"
and use it. Some people will think that is bad programming, but where you
declare a variable really depends on your programming style and other issues
like: am I running out of stack space, etc.
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/