Hello,
I am having a problem that I have spent days trying to figure out with no
success.  My program includes a form with two lists, the options list and
the picked list.  The options list is filled when the form loads and by
selecting items from the options list and clicking a button, the user can
fill their personal "picked list."
There is something slightly wrong with my function for adding to the picked
list however.  If I use the function as is (shown below) I end up with a
number of memory leaks equivalent to the number of list items added after
the first item.  (so I know that the leak is within the code in the
outermost Else clause of AddPickedItem).  I thought that the problem was
that I was setting the list pointer equal to the pointer for a newly created
string, but never dealloating the memory that was held by the previous
version of the list.  So I added a line (now commented) to free that memory.
However, when I add that line, my function ClearPickedList no longer works
upon closing the form.  (It does however work when called at the beginning
of FillList each time a new item is added to the picked list.)  I can an
unallocated chunk error.  Any Ideas on this?  My code is all below.
on a side note, when there are memory leaks, the emulator alerts me and
tells me that details can be found in "the log file."  What log file?

thank you!
meg
***********************code*********************************

void AddPickedItem(char* itemToAdd) {

    ListType *lstP;
    UInt16 newSize;
    char* newStringP;
    char* oldP;
    char* oldP2;
    UInt16 addingSize;
    UInt16 oldSize;
    UInt16 i;
    UInt16 length;
    Boolean alreadyThere=false;

    //grab pointer to picked list
    lstP=(ListType *)GetObjectPtr(DetailHasslesPickedList);

    //if picked list is currently empty
    if (pickedListStringP.numItems==0) {

     pickedListStringP.stringListP=itemToAdd;

    }//if (pickedListStringP.numItems==0)

    //if picked list already has items
    else {

     //calculate new size of string list
     oldSize = MemPtrSize(pickedListStringP.stringListP);
     addingSize=StrLen(itemToAdd);
     newSize=oldSize+addingSize+1;

     //create new string
     newStringP=MemPtrNew(newSize);

     //store old newString pointer
     oldP=newStringP;

     //store old picked list pointer
     oldP2=pickedListStringP.stringListP;

     //cycle through old items
     for(i=0;i<pickedListStringP.numItems;i++) {

      //find length of current item
      length=StrLen(pickedListStringP.stringListP);

      //copy old item into string
      StrCopy(newStringP,pickedListStringP.stringListP);

      //check to see if this item is not same as item being added
      if (StrCompare(pickedListStringP.stringListP, itemToAdd)==0) {

       //set flag
       alreadyThere=true;

       //alert user
       FrmAlert(AlreadyInListAlert);

      }//if (StrCompare(pickedListStringP.stringListP, itemToAdd)==0)

      //add nullterminator
      *(newStringP+length)='\0';

      //move pointers
      newStringP+=length+1;
      pickedListStringP.stringListP+=length+1;

     }//for(i=0;i<pickedListStringP.numItems;i++)

     //if item is unique, add to list and reset pointer
     if (!alreadyThere) {

      //copy in new item
         StrCopy(newStringP,itemToAdd);

      }//if (!alreadyThere)

     //reset pointers
     newStringP=oldP;

     //free memory used by old list
//if (oldP2)
   //   MemPtrFree(oldP2);

     //set list pointer to point to new list
     pickedListStringP.stringListP=newStringP;

    }//else

    //if item is unique
 if (!alreadyThere) {

  //update item count
  pickedListStringP.numItems++;

  //redraw list

FillPickedList(lstP,pickedListStringP.numItems,pickedListStringP.stringListP
);

  LstDrawList(lstP);

  LstSetSelection(lstP,LstGetNumberOfItems(lstP));

 }//if (!alreadyThere)

}//AddPickedItem

void FillPickedList(ListType *lstP, UInt16 numItems, char *stringP) {

 Char **itemList;
 UInt16 i;
 UInt16 length;

 //first free old memory used by this list
 ClearPickedList();

 //set aside memory for array of list items
 itemList = (Char **) MemPtrNew(numItems * sizeof(Char *));

 //save so memory can be released later
 hasslesPickedListChoices = itemList;
 hasslesPickedListNumItems=numItems;

 //for each list item
 for(i=0;i<numItems; i++) {

  length=StrLen(stringP);

  //create space in array
  itemList[i]=(char*)MemPtrNew(length+1);

  //write to array
  StrCopy(itemList[i], stringP);

  //move pointer to next item in string
  stringP+=(length+1);

 }//for(i=0;i<numItems; i++)

 //set list to reference array
 LstSetListChoices(lstP, itemList, numItems);

}//FillPickedList


void ClearPickedList(void) {

 //if there are still list items stored (from filling a list earlier)
 if (hasslesPickedListChoices) {

  UInt16 i;

  //for each item
  for(i=0;i<hasslesPickedListNumItems; i++) {

   //free item's memory
   MemPtrFree((MemPtr) hasslesPickedListChoices[i]);

  }//for(i=0;i<hasslesPickedListNumItems; i++)

  //free beginning pointer
  MemPtrFree((MemPtr)hasslesPickedListChoices);

  hasslesPickedListChoices=0;

 }//if (hasslesPickedListChoices)

}//ClearPickedList






-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to