Article View: pilot.programmer
Article #25freeing memory from uneditable field
From: 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
Technical Details
Message-ID:
<30890494.0209161101.52834dd7@posting.google.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10baa0,3aff17f5064426b6,start X-Google-Attributes: gid10baa0,public X-Google-Thread: 10c638,3aff17f5064426b6,start X-Google-Attributes: gid10c638,public X-Google-ArrivalTime: 2002-09-16 12:01:57 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mail From: david_ladanyi@yahoo.com (David Ladanyi) Newsgroups: pilot.programmer,codewarrior.palm Subject: freeing memory from uneditable field Date: 16 Sep 2002 12:01:56 -0700 Organization: http://groups.google.com/ Lines: 92 Message-ID: <30890494.0209161101.52834dd7@posting.google.com> NNTP-Posting-Host: 140.163.202.32 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1032202916 25188 127.0.0.1 (16 Sep 2002 19:01:56 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 16 Sep 2002 19:01:56 GMT