F.Y.I.

I've found my memory leak and discovered an error in the CW6 tutorials...

I coppied excactly what the memo tutorials said about reading a record and
found a serious memory leak when reading REPEATEDLY from a database.

//==========================================================================
===
//Old function
static void ReadRecord(DmOpenRef dbP, char *aRecord, int aRecordIndex)
{
        CharPtr newText, recText;
        Handle  newHandle;
        VoidHand        recHandle;
        
        // Check to make sure there is a record to retrieve.
        if (aRecordIndex != noRecordSelected && DmNumRecords(dbP) > 0)
                {
                // Get a handle for the first record.
                recHandle = DmGetRecord(dbP, aRecordIndex);
                if (recHandle == NULL) {
                  StrCopy(aRecord,"");
                  return;
                }

                // Lock down the handle and get a pointer to the record
data.
                recText = (CharPtr)MemHandleLock(recHandle);
                
                // Allocate a new memory chunk that will contain a copy of
the data.
                newHandle = (Handle)MemHandleNew(StrLen(recText) + 1);
                
                // Lock down the handle and get a pointer to the memory
chunk.
                newText = (CharPtr)MemHandleLock(newHandle);
                
                // Copy the data from the record to the new memory chunk.
                StrCopy(newText, recText);
            StrCopy(aRecord,newText);
        
                // Unlock the new memory chunk.
                MemHandleUnlock(newHandle);
                
                // Unlock the database record.
                MemHandleUnlock(recHandle);
                
                // Release the record to the database system.
                // (The zero means that the record data hasn't been
changed.)
                DmReleaseRecord(dbP, aRecordIndex, 0);          
                }
}


this is how i fixed it and it is working fine now:

//==========================================================================
===
//New function
static void ReadRecord(DmOpenRef dbP, char *aRecord, int aRecordIndex)
{
        CharPtr recText;
        VoidHand        recHandle;
        
        // Check to make sure there is a record to retrieve.
        if ((aRecordIndex >= 0) && (aRecordIndex < DmNumRecords(dbP))) {
                // Get a handle for the first record.
                recHandle = DmGetRecord(dbP, aRecordIndex);
                if (recHandle == NULL) {
                  StrCopy(aRecord,"");
                  return;
                }

                // Lock down the handle and get a pointer to the record
data.
                recText = (CharPtr)MemHandleLock(recHandle);
                                
                // Copy the data from the record to the new memory chunk.
                StrCopy(aRecord, recText);
                
                // Unlock the database record.
                MemHandleUnlock(recHandle);
                
                // Release the record to the database system.
                // (The zero means that the record data hasn't been
changed.)
                DmReleaseRecord(dbP, aRecordIndex, 0);
                
        }
}

Regards,
Herman

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

Reply via email to