🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: pilot.programmer.codewarrior
5 messages
5 total messages Started by David Ladanyi Mon, 07 Oct 2002 06:56
char* memory leak with emulator 3.5
#22
Author: 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
Re: char* memory leak with emulator 3.5
#23
Author: Baxter
Date: Mon, 07 Oct 2002 14:15
61 lines
3130 bytes
Somewhere in your code, you need a:

delete psaStr;

--
-------------------------------------------------------------------------
Free software  - Baxter Codeworks  www.baxcode.com
-------------------------------------------------------------------------


"David Ladanyi" <david_ladanyi@yahoo.com> wrote in message
news:30890494.0210062256.7a8a8fa@posting.google.com...
> 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
Re: char* memory leak with emulator 3.5
#24
Author: David Ladanyi
Date: Tue, 08 Oct 2002 17:05
68 lines
3556 bytes
When I put in a "delete psaStr" line it produced a memory error
because the pointer was being used by the field. I managed to fix the
problem by declaring "char psaStr[6];" instead of "char* psaStr = new
char[6];"


"Baxter" <baxter.spamblock@baxcode.com> wrote in message news:<uq35nhqhshk23@corp.supernews.com>...
> Somewhere in your code, you need a:
> 
> delete psaStr;
> 
> --
> -------------------------------------------------------------------------
> Free software  - Baxter Codeworks  www.baxcode.com
> -------------------------------------------------------------------------
> 
> 
> "David Ladanyi" <david_ladanyi@yahoo.com> wrote in message
> news:30890494.0210062256.7a8a8fa@posting.google.com...
> > 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
Re: char* memory leak with emulator 3.5
#25
Author: Ben Combee
Date: Wed, 09 Oct 2002 11:34
15 lines
1958 bytes
In article <30890494.0210080905.64899638@posting.google.com>, 
david_ladanyi@yahoo.com says...
> When I put in a "delete psaStr" line it produced a memory error
> because the pointer was being used by the field. I managed to fix the
> problem by declaring "char psaStr[6];" instead of "char* psaStr = new
> char[6];"

Is psaStr a local variable or a global?  If its local, you might have 
just passed a pointer to stack space into a global structure -- this is 
bad, as that space can and will be used for other variables after the 
current function exits.
-- 
Ben Combee <bcombee@metrowerks.com>
CodeWarrior for Palm OS technical lead
Get help at http://palmoswerks.com/
Re: char* memory leak with emulator 3.5
#26
Author: David Ladanyi
Date: Thu, 10 Oct 2002 05:03
20 lines
2193 bytes
psaStr is a local variable.  Where and how do I deallocate the memory
so that I dont get a memory leak? Apparently the Palm OS will not
deallocate the memory itself when the field contain dynamic memory and
the clean up function I put in (called on frmCloseEvent) seemed to do
nothing. If I'm using the wrong strategy then what am I supposed to
do?
 

Ben Combee <combee@techwood.org> wrote in message news:<MPG.180dcfbc414a20d79898c2@news.earthlink.net>...
> In article <30890494.0210080905.64899638@posting.google.com>, 
> david_ladanyi@yahoo.com says...
> > When I put in a "delete psaStr" line it produced a memory error
> > because the pointer was being used by the field. I managed to fix the
> > problem by declaring "char psaStr[6];" instead of "char* psaStr = new
> > char[6];"
> 
> Is psaStr a local variable or a global?  If its local, you might have 
> just passed a pointer to stack space into a global structure -- this is 
> bad, as that space can and will be used for other variables after the 
> current function exits.
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