🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pilot.programmer.codewarrior
Article #22

char* memory leak with emulator 3.5

#22
From: David Ladanyi
Date: Mon, 07 Oct 2002 06:56
49 lines
2570 bytes
Hi

I have a program that seems to run fine in the emulator except that
whenever I try to close the program I'm told that I have a memory
leak. I've figured out what statement is causing the leak but I can't
figure out how to fix it. This code is called when a certain form is 
being initialized:

double psaVal = pronData->pretx_psa;
char* psaStr = new char[6];
	
if(psaVal != NO_VALUE) {
	doubleToStr(psaStr, psaVal, 1);
}
else
	psaStr = "";
	
SetFieldHandle(GetObjectPtr<FieldType>( PreTxPSAField), psaStr);

This code simply creates a string from a double and sets a field to
that string. The memory leak was caused by the "new char[6]" line so I
put in a deallocation function that would be called when the form
receives a frmCloseEvent (I made sure to set handled=false):

void PreTxCleanUp() {

        FormType* frmP = FrmGetActiveForm();
	FieldType* field = (FieldType*)
FrmGetObjectPtr(frmP,FrmGetObjectIndex (frmP, PreTxPSAField));
	
	MemHandle refs = FldGetTextHandle(field);
	
	if(refs != NULL) {//  && MemHandleUnlock(refs) != memErrInvalidParam)
{
		FldSetTextHandle (field, NULL);
		MemHandleFree(refs);
	}
	
}

This function doesnt give any errors buy it also doesn't fix the
problem. Every time I open and close the form in question it creates a
leaked memory chunk. Am I not de-allocating the memory properly or is
there a problem with the emulator? Also, if the memory leak is caused
by a variable local to a certain form why isn't the emulator telling
me about the leak as soon as I close the form as opposed to waiting
until I close the whole program? Thanks in advance for any advice.

Dave
Technical Details
Message-ID: <30890494.0210062256.7a8a8fa@posting.google.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 109d41,fc0bc5958a8d445f,start
X-Google-Attributes: gid109d41,public
X-Google-Thread: 10baa0,fc0bc5958a8d445f,start
X-Google-Attributes: gid10baa0,public
X-Google-ArrivalTime: 2002-10-06 23:56:13 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,codewarrior.palm
Subject: char* memory leak with emulator 3.5
Date: 6 Oct 2002 23:56:12 -0700
Organization: http://groups.google.com/
Lines: 49
Message-ID: <30890494.0210062256.7a8a8fa@posting.google.com>
NNTP-Posting-Host: 209.244.243.136
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1033973772 24604 127.0.0.1 (7 Oct 2002 06:56:12 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: 7 Oct 2002 06:56:12 GMT