I am creating a dynamic list in my application, filling the list with records from a database.
Everything works well except sometimes at the end of the list I see garbage characters. What am I doing wrong below? Here is the code:
//Global variables char **ListChoices=0; Int16 ListNumItems; //Global variables end
//Local variables Char **itemList; records_record *structPtr; //Local variables end
ListNumItems = DmNumRecords(records_StDd); // records_StDd is the DB name
Problem 1: DmNumRecords includes deleted and archived records in its count. If that's a problem, use DmNumRecordsInCategory as directed in the SDK documentation.
if(ListNumItems > 0) { itemList = (Char **) MemPtrNew(ListNumItems * sizeof(Char *)); ListChoices = itemList; // save so we can free it later
for(i = 0; i <= ListNumItems-1; i++) { structHandle = DmGetRecord(records_StDd, i);
Problem 2: if your are just reading a record, use DmQueryRecord, not DmGetRecord.
structPtr = MemHandleLock(structHandle);
if(structPtr->fld_1 != NULL && structPtr->fld_1 != 0x00) //structPtr->fld_1 is the field value from DB { itemList[i] = (char *) MemPtrNew(100); StrNCopy(itemList[i], structPtr->fld_1, 100); // only copy up to 100 bytes itemList[i][99] = 0x00; // take care about the 0x00 at the end of the string }
Problem 3: there's no negative case on this if statement that sets itemList[i] to an empty string or some sentinel value when you look at a record you're ignoring.
MemHandleUnlock(structHandle); DmReleaseRecord(records_StDd, i, 0); }
if (ListChoices) { for (i=0; i < ListNumItems; i++) MemPtrFree((VoidPtr) ListChoices[i]);
MemPtrFree((VoidPtr) ListChoices); ListChoices=0; }
lstP = (ListType *) FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, AddLocationDayList)); listptr = (ListPtr)FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, AddLocationDayList)); LstSetListChoices(listptr, itemList, ListNumItems); LstDrawList(listptr);
I'd also suggest that this would be better implemented using a list drawing callback, rather than making a copy of all that data.
-- Ben Combee, Technical Lead, Developer Services, PalmSource, Inc. "Combee on Palm OS" weblog: http://palmos.combee.net/ Developer Fourm Archives: http://news.palmos.com/read/all_forums/
-- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
