What I meant was, if you throw away the strArray pointer, you won't know
which memory to free when you get called again.
I'd modify your code to do something like this, sorry if there's any
typos in it, but it shows you the general gist.
I'd also be adding a bit more error checking to your code...
search_database()
{
Char *p;
// make this pointer static, we need it later
static Char **strArray = NULL;
Char* strLines = NULL;
Err err;
UInt16 index = 0, recordindex = 0, nRecordIndex = 0;
FormPtr form1;
ListType *lsp1;
MemHandle Rh;
// Check to see if we've been called before
// (if we have not, strArray will have been initialised to NULL)
if(strArray)
{
// We've been called before, so free ALL the memory we
borrowed last time
// We need a counter, May as well use recordindex,
because it's there
for(recordindex=0;recordindex<(MemPtrSize(strArray)/sizeof(Char
*));recordindex++)
{
// Is this pointer valid?
if(strArray[recordindex])
{
// It is a valid pointer, so free it
MemPtrFree(strArray[recordindex]);
// Just for neatness!
strArray[recordindex]=NULL;
}
}
// Now free the overall pointer
MemPtrFree(strArray);
// Just for neatness
strArray=NULL;
recordindex=0;
}
// We're clean again now, the OS has ALL it's memory refunded!
form1 = FrmGetActiveForm(); // get active form
err = Open(); // routine to open database
if(!err)
{
lsp1 = FrmGetObjectPtr(form1, FrmGetObjectIndex(form1,
frmList1)); // get id of list control
nRecordIndex = DmNumRecords(dbPtr); // number of records
strArray = MemPtrNew(nRecordIndex * sizeof(Char *));
for(index =0; index < nRecordIndex; index++) // lets index
through database
{
Rh = DmQueryRecord(dbPtr, index); // lets query each record
p = (Char *) MemHandleLock(Rh); // lets lock each record
if( 0 == StrNCaselessCompare( letter, p, 1) ) //compare first
letter of first field
{
strArray[recordindex] = MemPtrNew(StrLen(p) + 1);
StrCopy(strArray[recordindex], p);
recordindex++;
}
MemHandleUnlock(Rh); // unlock each record
}
LstSetListChoices(lsp1,strArray,recordindex);
LstDrawList( lsp1 );
err = CloseDB();
}
return true;
}
--
For information on using the PalmSource Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/support/forums/