At 01:12 PM 19/3/2002 -0800, you wrote:
>I have a question regarding pointers and handles...  When exiting my 
>application, CW says that I have 1 memory leak.  I was
>under the impression that for each handle I have, I should free it, and 
>for each pointer, I should free it when I am done with
>it.  The code below is what is giving me the problem.  It only happens if 
>I have records in the DB, i.e. the code makes it to
>the 'for loop'.
>
>I put the MemPtrFree(itemList); and MemHandleFree(recordH); in the code 
>(see the bottom of this email) but when I do, I get a
>fatal error.  My question is, where should I be doing the cleanup for this 
>handle?
>
>Thanks,
>Ed.
>
>void frmTypesInitForm() {
>     FormPtr        frmP = FrmGetActiveForm();
>     ListType       *lstP;
>     UInt16         theCategory = dmAllCategories;
>     UInt16         totalItems, i;
>     UInt16         recordNum = 0;
>     UInt16         pos = 9;
>     MemHandle      recordH;
>     Char           **itemList;
>
>     // List box
>     lstP = (ListType *) FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP, 
> frmTypesList));
>
>     // Get number of records in database
>     if(gSRCatDB != NULL){
>
>        totalItems = DmNumRecordsInCategory(gSRCatDB, theCategory);
>        itemList = (Char **) MemPtrNew(totalItems * 16);
>
>         // Code to visit every record:
>         for (i = 0; i < totalItems; i++) {
>             recordH = DmQueryNextInCategory (gSRCatDB, &pos, theCategory);
>
>             if (recordH) {
>                skelSRCatDBType * p = (skelSRCatDBType *) 
> MemHandleLock(recordH);
>                itemList[i] = (char *) &(p->ExType);
>
>                MemHandleUnlock(recordH);
>                pos++;
>             }
>        }
>
>        LstSetListChoices(lstP, itemList, totalItems);
>        //MemPtrFree(itemList);
>        //MemHandleFree(recordH);
>     }
>}

You definitely don't want to do the last MemHandleFree since you didn't 
call MemHandleNew. You were just assigned a handle to one of the records in 
your database. You do want to MemPtrFree on the itemList, however, you want 
to do this *after* you (and the list manager) have finished using it. You 
will have to figure out the best place to do this. Probably when processing 
your frmCloseEvent event (assuming you only call the above function when 
processing the frmOpenEvent).

Matt


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

Reply via email to