Boris Epshteyn wrote:
Hi,Boris,
I have an interesting problem. I am allocating an array of strings. This array is filled from the database and then used for populating pop-up list. When program is ran, list pops up fine, item gets selected, but when I tap on the trigger again I get: "just read from the ... which is the reserved field of the frmControlObj," bla-bla-bla.
If my clean-up portion of the code is commented out, everything runs fine, but creates memory leaks. Clean-up portion is: for (i = numRec - 1; i >= 0; i--) { MemPtrFree(programsArray[i]); } MemPtrFree(programsArray);
I would appreciate any ideas why it is happening. The code is bellow:
static Boolean frmNewM_trProg_OnSelect(EventPtr event)
{
ListType *list = GetObjectPtr(lstPrograms);
Int16 listItem, cardNo;
UInt32 numRec = 0;
Err err;
char** programsArray;
MemHandle record;
LocalID progLocID;
Program* prog;
int i;
MemPtr ptr;
err = DmOpenDatabaseInfo(progDB, &progLocID, NULL, NULL,&cardNo, NULL);
if (progLocID != 0)
{
err = DmDatabaseSize(cardNo, progLocID, &numRec, NULL, NULL);
}
programsArray = (char**)MemPtrNew(numRec * sizeof(char*));
for (i = 0; i < numRec; i++)
{ record = DmQueryRecord(progDB, (UInt16)i);
if (record)
{
prog = (Program*)MemHandleLock(record);
ptr = &prog->programName;
programsArray[i] = (char*)MemPtrNew(StrLen(ptr) + 1 );
StrCopy(programsArray[i], ptr);
MemHandleUnlock(record);
}
}
LstSetListChoices(list, programsArray, numRec);
listItem = LstPopupList(list); if(listItem != -1)
{
ControlType *trg = GetObjectPtr(trProg);
CtlSetLabel(trg, programsArray[listItem]);
is CtlSetLabel actually copying the string or just storing a reference to it (can't look it up right now)? If it stores just a reference to the string you can't free the memory before the label is destroyed
} for (i = numRec - 1; i >= 0; i--) { MemPtrFree(programsArray[i]); } MemPtrFree(programsArray); return true; }
-- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
