At first glance, if your fld_1 value is NULL or an empty string, you
won't be setting anything for itemList[i], I think you may want an else
- that's my first guess
without thinking at all (disclaimer) it might look like this...
if(structPtr->fld_1 != NULL && structPtr->fld_1 != 0x00)
{
itemList[i] = (char *) MemPtrNew(100);
StrNCopy(itemList[i], structPtr->fld_1, 100);
itemList[i][99] = 0x00; // take care about the 0x00 at the end of
}
else // take care about NO STRING AT ALL
{
itemList[i] = (char *) MemPtrNew(100);
itemList[i][0] = 0x00;
}
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Developer
Sent: 24 November 2004 20:00
To: Palm Developer Forum
Subject: Question about lists
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
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);
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
}
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);
--
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/