I decided to dig up my program again and make a very simple project
to illustrate what I have done. It also fills a list dynamically,
but there are no memory leaks resulting from this code. You can
easily copy this code just as it is into a small project and try it
out. I have not really compared this with your code to see what is
different, but having a working sample always makes it easier to find
the fault in your own code.
(Oh, and I think you are right... it makes no point doing the cleanup
in AppStop as well.)
Regards
Werner Terreblanche
Here is my short version code to fill and clean a list:
// Resource File : (PilRc)
FORM ID MainForm AT (0 0 160 160)
NOSAVEBEHIND NOFRAME
MENUID MainMenuBar
BEGIN
TITLE "test2"
GRAFFITISTATEINDICATOR AT (149 148)
POPUPTRIGGER "" ID MyPopup AT (22 64 AUTO AUTO)
LIST ID MyList AT (22 64 126 46) VISIBLEITEMS 5 NONUSABLE
POPUPLIST MyPopup MyList
BUTTON "Create List" ID CreateListButton AT (1 147 AUTO 12)
END
ALERT ID MessageBoxInfoAlert
INFORMATION
BEGIN
TITLE "Information"
MESSAGE "^1 ^2 ^3"
BUTTONS "OK"
END
/*********************************************************************
* Global variables
*********************************************************************/
static char **gDatabaseListChoices=0; // The resource handle and the
newly allocated array for list
static Int16 gDatabaseListNumItems; // Remember to deallocate these
when the form closes.
static Boolean MainFormHandleEvent(EventType * eventP)
{
Boolean handled = false;
FormType * frmP = FrmGetActiveForm();
ListType *lstP;
Char SelItemTxt[20];
UInt16 i;
switch (eventP->eType)
{
case menuEvent:
case frmOpenEvent:
FrmDrawForm(frmP);
handled = true;
break;
case frmUpdateEvent:
/*
* To do any custom drawing here, first call
* FrmDrawForm(), then do your drawing, and
* then set handled to true.
*/
break;
case ctlSelectEvent:
{
if (eventP->data.ctlSelect.controlID ==
CreateListButton)
{
lstP = (ListType *) FrmGetObjectPtr
(frmP, FrmGetObjectIndex(frmP, MyList));
FillListWithContents(lstP);
break;
}
break;
}
case popSelectEvent:
StrCopy(SelItemTxt,LstGetSelectionText(eventP-
>data.popSelect.listP, eventP->data.popSelect.selection));
FrmCustomAlert(MessageBoxInfoAlert, "You
selected ", SelItemTxt, 0);
break;
case frmCloseEvent:
// prevent Memory leaks
if (gDatabaseListChoices) {
for (i=0; i<gDatabaseListNumItems;
i++)
{
MemPtrFree((MemPtr)
gDatabaseListChoices[i]);
}
MemPtrFree((MemPtr)
gDatabaseListChoices);
gDatabaseListChoices=0;
}
frmP = FrmGetActiveForm();
FrmEraseForm (frmP);
FrmDeleteForm (frmP);
handled = true;
break;
}
return handled;
}
static Err FillListWithContents(ListType *lst)
{
Char **itemList;
UInt16 NrListItems = 3;
itemList = (Char **) MemPtrNew(NrListItems * sizeof(Char *));
itemList[0] = (char *) MemPtrNew(StrLen("One")+2);
StrCopy(itemList[0],"One");
itemList[1] = (char *) MemPtrNew(StrLen("Two")+2);
StrCopy(itemList[1],"Two");
itemList[2] = (char *) MemPtrNew(StrLen("Three")+2);
StrCopy(itemList[2],"Three");
LstSetListChoices(lst, itemList, NrListItems);
gDatabaseListChoices = itemList; // save so we can free it
later in frmCloseEvent
gDatabaseListNumItems = NrListItems;
LstSetSelection(lst, 1);
return 0;
}
--- In [EMAIL PROTECTED], "meg" <[EMAIL PROTECTED]> wrote:
> Thank you very much for your response. Unfortunately I'm still
stuck.
>
> Actually, the problem is more that ClearPickedList doens't work
correctly
> once I do free the memory. I'm pretty sure about where the memory
leaks are
> coming from, but fixing them breaks ClearPickedList (and only on
the last
> call). As for putting ClearPickedList in AppStop, I have it in the
exit
> cleanup routine for this form, since that would take care of the
memory held
> by the form's list. Once the form has been dismissed, that list no
longer
> exists, so it'd never tie up any more memory than what it had upon
dismissal
> of the form. So any cleanup for this list actually has to go in
the cleanup
> for the form and isn't really helpful in AppStop.....
>
> My problem is mainly that I add a picked item and call fill list
which first
> calls clear list. This works repeatedly, but then once I try to
exit the
> form, and run ClearList without first adding a new item ClearList
doesn't
> work.
>
> ie:
> AddPickedItem
> Clear List
> Fill List
>
> AddPickedItem
> Clear List
> Fill List
>
> AddPickedItem
> Clear List
> Fill List
>
> ClearList (this call doesn't work, memory has been deallocated)
>
> The obvious difference between this call to Clear List and the
working ones
> is that they all have a preceding AddPickedItem, but I just can't
see where,
> in AddPickedItem, the code is that makes ClearList work.....
>
>
>
> --
> For information on using the Palm Developer Forums, or to
unsubscribe, please see http://www.palmos.com/dev/support/forums/
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/support/forums/