At 04:16 AM 5/27/2006, you wrote:
Subject: Re: Memory Leak
From: "Carlos Gonzalez" <[EMAIL PROTECTED]>
Date: Fri, 26 May 2006 20:05:45 -0000

Here is part of my code and it is the only place where I use MemHandleLock and unlock.. that is it.

Boolean CalculateRisk()
{
ControlType* ctl;
Boolean         handled = true;
MemHandle       textH;
char    *str;
double  PI;
...
PI += -1.236215274 - 2.598564382 - 2.119863737 + 8.600789247;
PI = exp(PI);
PI = (1.0 - pow(0.906,PI))* 100.0;
str = MemHandleLock(textH);
doubleToStr(str, PI, 2);
MemHandleUnlock(textH);
FldSetTextHandle(GetObjectPtr(MainFieldRisk),textH);
FldDrawField(GetObjectPtr(MainFieldRisk));
MemHandleFree(textH);
return handled;
}


You show the MemHandleFree, but not the MemHandleNew. How do you get the handle you free? Also you assign the handle to a field, and then free it ? Naughty naughty since the field is now using a free'd handle and therefore a chuck of memory about to be used by some other code.

Also what happened to the old handle that was attached to that field????
I suspect this is the source of your memory leak.

This code also has a total absence of error handling. The following is better code:
str = MemHandleLock (textH):
if (str) {
        doubleToStr (str, PI, 2);
        MemHandleUnlock (textH);
} else FatalErrorAlert ("Couldn't lock textH");


Roger Stringer
Marietta Systems, Inc. (www.rf-tp.com)


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

Reply via email to