On Jan 11, 2005, at 10:55 PM, Logan Shaw wrote:
David Linker wrote:When I run it in POSE with gremlins, after about 7000 events, I get 4 memory leaks occuring in the routine which places the strings in the Fields, even though the Fields have been populated perhaps hundreds of times by then.: :void setFieldTextFromString(Word fieldID, CharPtr s) { Handle textH; FormPtr frm = FrmGetActiveForm(); FieldPtr f; CharPtr textP;
f = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm,fieldID)); FldFreeMemory(f); textH = MemHandleNew(StrLen(s) +1); textP = MemHandleLock(textH); StrCopy(textP,s); FldSetTextHandle(f,textH); MemHandleUnlock(textH); FldDrawField(f); }
I'm not sure why exactly that is happening. It seems like the field should be inactive between the time the FldFreeMemory() call is made and the time the FldSetTextHandle() call is made. But, if somehow the field is doing some processing, then it seems possible some
However, one thing you can try is this: the API docs have some sample code that recommends doing a FldGetTextHandle() then doing a FldSetTextHandle() if you want to change a field's contents. That would make your function look something like this:
void setFieldTextFromString (Word fieldID, CharPtr s) { MemHandle oldH, newH; FormPtr frm = FrmGetActiveForm(); FieldPtr f; CharPtr textP;
newH = MemHandleNew (1 + StrLen (s)); if (newH == NULL) { return; } textP = (CharPtr) MemHandleLock (newH); StrCopy (textP, s); MemHandleUnlock (newH);
f = FrmGetObjectPtr (frm, FrmGetObjectIndex (frm, fieldID)); oldH = FldGetTextHandle (f); FldSetTextHandle (newH);
if (oldH != NULL) { MemHandleFree (oldH); }
FldDrawField (f); }
Not sure if that will fix the problem, but it's an idea.
- Logan
Thank you for the reply. That is actually what my original code looked like, since I copied it from an example. It also caused the same problem.
DTL
-- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
