Thread View: pilot.programmer
2 messages
2 total messages
Started by David Ladanyi
Mon, 16 Sep 2002 19:01
freeing memory from uneditable field
Author: David Ladanyi
Date: Mon, 16 Sep 2002 19:01
Date: Mon, 16 Sep 2002 19:01
92 lines
3353 bytes
3353 bytes
Hi
I'm cant seem to free the memory I've allocated for a string that I'm
using in an uneditable field. Here's how I'm initializing it:
char* PreTxGetReferences() {
int refLength = 0;
for(int i=0; i<RPProstogram::numReferences; i++)
refLength += StrLen(RPProstogram::references[i]);
refLength += RPProstogram::numReferences * 2;
char* refStr = new char[refLength + 1];
StrCopy(refStr, "");
for(int i=0; i<RPProstogram::numReferences; i++) {
StrCat(refStr, RPProstogram::references[i]);
StrCat(refStr, "\n\n");
}
return refStr;
}
the string returned by this method is used in the following to set the
field value:
void SetFieldHandle(FieldType* field, char* textPtr) {
//FieldType* field = GetObjectPtr<FieldType>(fieldName);
Err err;
MemPtr p;
MemHandle h = FldGetTextHandle(field);
if (h) {
FldSetTextHandle(field, NULL);
err = MemHandleResize(h, StrLen(textPtr) + 1);
if (err != 0) {
FldSetTextHandle(field, h);
//return err;
return;
}
} else {
h = MemHandleNew(StrLen(textPtr) + 1);
if (!h)
//return memErrNotEnoughSpace;
return;
}
p = MemHandleLock(h);
StrCopy((char*)p, textPtr);
MemHandleUnlock(h);
FldSetTextHandle(field, h);
FldDrawField(field);
return;
}
and here's how I'm trying to deallocate it when the form closes:
void ReferencesFormCleanup() {
FormType* frmP = FrmGetActiveForm();
FieldType* field = (FieldType*) FrmGetObjectPtr(frmP,
FrmGetObjectIndex(frmP, ReferencesRefsField));
/*
MemHandle fldHandle = FldGetTextHandle(field);
if(fldHandle != NULL && MemHandleUnlock(fldHandle) !=
memErrInvalidParam) {
MemHandleFree(fldHandle);
}
*/
MemPtr refs = FldGetTextPtr(field);
if(refs != NULL && MemPtrUnlock(refs) != memErrInvalidParam) {
MemPtrFree(refs);
}
}
I've tried using both the commented out MemHandle block and the MemPtr
block below it. Both give the same error - SysFatalAlert, MemoryMgr.c
line 4359 Free handle. When I dont deallocate the string I get an
error from the emulator when I try to close the program saying that
there was a memory leak detected. When I take out the MemPtrUnlock and
MemHandleUnlock calls I still get the same error. What am I doing
wrong here? Thanks in advance for any advice.
Dave
Re: freeing memory from uneditable field
Author: Ben Combee
Date: Tue, 17 Sep 2002 05:10
Date: Tue, 17 Sep 2002 05:10
23 lines
1964 bytes
1964 bytes
In article <30890494.0209161101.52834dd7@posting.google.com>, david_ladanyi@yahoo.com says... > Hi > > I'm cant seem to free the memory I've allocated for a string that I'm > using in an uneditable field. Here's how I'm initializing it: First, it its an uneditable field, then just use FldGetTextPtr and FldSetTextPtr. You don't need to mess with field handles unless the field is marked as editable. One key thing is to make sure that when you're deallocating the field's text, you do it in this order: 1) ptr = FldGetTextPtr 2) FldSetTextPtr(NULL) 3) MemPtrFree(ptr) Setting the internal pointer to NULL prevents a double free situation. -- Ben Combee <bcombee@metrowerks.com> CodeWarrior for Palm OS technical lead Get help at http://palmoswerks.com/
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads