The problem is with LstSetListChoices. At some point you'll have to
deallocate the memory that's pointed to be itemList. The doc for
LstSetListChoices spells this out.
You do need to do a MemPtrFree on itemList (or at least on that bit
of memory); you just need to wait until the form closes to do it.
You should not be calling MemHandleFree on recordH because recordH is
a handle to a record in your database.
I noticed several other problems in the code, btw. Inside your loop,
suppose the DmQueryNextInCategory returns NULL. That means that
"pos" never gets incremented. The next time through the loop, the
parameters to DmQueryNextInCategory haven't changed. Thus, once you
get one NULL from DmQueryNext..., you won't be setting any values for
itemList[i] for the rest of the loop. Is that what you want?
Second, you're setting itemList[i] to a field of a record referenced
by a locked handle. Once you unlock that handle, the pointer
itemList[i] is invalid; that record might get moved around.
Third, there's a mystery value in your call to MemPtrNew. "16" is
presumably the size of some structure; better to do a sizeof() call,
or #define a parameter that specifies string length, or whatever.
Finally (maybe I should have said this first), you should ask what
happens with your approach when the number of records in your
database gets large. You are requiring a pretty big chunk of memory
to store all of those strings, just to put them into a list. And
this is for one item on one form of your app. Better to do a
dynamically drawn list, with a callback that fetches the record and
draws the ExType.
At 1:12 PM -0800 3/19/02, Edward P. Ross 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);
> }
>}
>
>
>
>--
>For information on using the Palm Developer Forums, or to
>unsubscribe, please see http://www.palmos.com/dev/support/forums/
----
Hal Mueller [EMAIL PROTECTED]
Mobile Geographics LLC http://www.mobilegeographics.com/
Seattle, Washington (206) 297-9575
MapTap public test now underway!
http://www.mobilegeographics.com/maptap/
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/support/forums/