> MemPtrFree disposes a non-movable chunk of memory. After you free the
> memory, what makes you think that you can later use the
> pointer to that
> memory? (A: you can't, unless the memory hasn't yet been
> over-written with something else.)
>
> If you use a gadget to store a pointer to some data, you
> should make sure
> that the data it is pointing to remains unchanged as long as
> you need it.
Makes sense, but how long is that, exactly? If I have two forms in my
application, and the main form initializes the data in my gadgets, and the
other form reads data from and writes data to the gadgets (but doesn't
display the gadgets), how do I get to the gadgets' data when I'm on the
second form? Example: I have 5 gadgets on the main screen, and each gadget
contains 10 data elements (the data pointer for each gadget points to a
struct with 10 elements). When I tap any gadget on the main screen, I want
to go to my other screen, and display all 10 data elements for that gadget,
and be able to edit them, and when I return to the main screen, I want the
changes made to be saved back to the gadget.
So, I initialize the gadgets with an initializing function on the main form,
reading the data from a database (which has been hot synched from a desktop
application, say). Clearly, when I do that, I have to allocate a pointer for
each of my 5 gadgets, with MemPtrNew being called 5 different times, right?
But, as you pointed out above, I can't do MemPtrFree on those 5 pointers, or
I'll lose my data. So I tap on a gadget to go to my editing form for the
tapped gadget, and I'm going to be getting (for display) and setting (for
saving any changes) data from and to the gadget. How do I do that? Since the
gadgets "live" on the main form, and since FrmGetGadgetData and
FrmSetGadgetData require a FormPtr to the form on which they "live," how do
I do that from my editing form? The obvious answer is to pass in the pointer
to the main form in the function call that brings up the second form, but
can you do that? Pass a FormPtr around like that? And if so, is there
anything special you have to do to prevent the OS from moving it? Like lock
it somehow? (See, this is where I get totally fuzzy...)
So, let's say we get past that hurdle, and we get our editing form properly
displaying and updating the gadget's data. When I return to the main form, I
still need to be able to read the gadgets' data, because I want to display
one element of the data like a label right on the gadget, so I can't do a
MemPtrFree just yet. In fact, the only time I'm really going to be done with
those pointers is when I exit the application, for good. So do I put my
MemPtrFree code in my AppStop( ) function?
Also (I know, this is a lot of questions...) - Right now, my initialization
of the gadgets occurs whenever a frmOpenEvent is received by my main form
event handler, and that's going to happen every time I switch back to the
main form from the editing form, won't it? Or, once a form is created, does
the OS not actually destroy it each time you leave it, but rather just hide
it? If it just hides it, that's cool - I won't be doing a bunch of extra
MemPtrNew's every time I return to the main form. But if the OS actually
destroys the form, and has to recreate it each time it's to be displayed,
then I need to put my MemPtrFree's in the code that gets called whenever one
form is closed so that another can be displayed. Do I do that by adding a
function to my main form event handler to handle the frmCloseEvent - put the
MemPtrFree's in there? But again, if I do that, I've now destroyed my
pointers to my gadgets' data, making it unavailable from any other form. So,
do I make my gadgets and their data global somehow? But how? Gadgets are
resources on forms, right? I have to have a FormPtr to either Get or Set
gadget data. But if that form is destroyed every time I leave it, how can I
keep the gadgets and their data around? There has to be a way, or gadgets
would be of very limited use...
Sorry to ask so many questions, but I'm really getting into this, and these
memory-related roadblocks are making me crazy!
Thanks a million,
Brian Smith
> -----Original Message-----
> From: Richard Burmeister [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, May 15, 2001 6:08 PM
> To: Palm Developer Forum
> Subject: RE: Avoiding globals
>
>
> > From: Brian Smith
> >
> > void InitOneItem(FormPtr pForm, UInt8 nCurrentItem, UInt16
> nGadgetIndex) {
> >
> > UInt8 *pItem = MemPtrNew(sizeof(UInt8));
> > if (pItem != NULL) {
> > *pItem = nCurrentItem;
> > FrmSetGadgetData(pForm, nGadgetIndex, pItem);
> > }
> > MemPtrFree(pItem);
> > }
> >
>
>
> --
> For information on using the Palm Developer Forums, or to
> unsubscribe, please see http://www.palmos.com/dev/tech/support/forums/
>
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/