BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Release memory allocated by function result

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc
View previous topic :: View next topic  
Author Message
DI Konsult
Guest





PostPosted: Fri Feb 06, 2004 5:16 pm    Post subject: Release memory allocated by function result Reply with quote



Following code:
function DoSomeThingBitmap(Bitmap: TBitmap): TBitmap;
begin
....
Result := TBitmap.Create;
....
end;

var
Bmap:TBitMap;

begin
Back to top
Bruce Roberts
Guest





PostPosted: Fri Feb 06, 2004 5:34 pm    Post subject: Re: Release memory allocated by function result Reply with quote




"DI Konsult" <dikonsult_1 (AT) telia (DOT) com> wrote

Quote:
Following code:
function DoSomeThingBitmap(Bitmap: TBitmap): TBitmap;
begin
...
Result := TBitmap.Create;
...
end;

var
Bmap:TBitMap;

begin
.
Bmap:=DoSomeThingBitmap(Bmap);

. . .
Bmap.Free;

Quote:
.
end.

Consumes memory each time the function is called.

Which is why IMO its a poor coding choice. Much better is

procedure DoSomethingBitmap (bmp : tBitmap);

begin
.. . .
end;

var Bmap : tBitmap;

begin
.. . .
Bmap := tBitmap.Create;
try
DoSomethingBitmap (bmp);
. . .
finally
Bmap.Free;
end;

IOW always try to code so that the nesting level that allocates a resource
also frees it.



Back to top
Rob Kennedy
Guest





PostPosted: Fri Feb 06, 2004 5:37 pm    Post subject: Re: Release memory allocated by function result Reply with quote



DI Konsult wrote:
Quote:
My question, how can I free the memory occpied by the result value of an
function?

By freeing it.

Bmap.Free;

--
Rob

Back to top
DI Konsult
Guest





PostPosted: Fri Feb 06, 2004 5:43 pm    Post subject: Re: Release memory allocated by function result Reply with quote


"Rob Kennedy" <me (AT) privacy (DOT) net> skrev i meddelandet
news:c00jc4$11o2gt$1 (AT) ID-220940 (DOT) news.uni-berlin.de...
Quote:
DI Konsult wrote:
My question, how can I free the memory occpied by the result value of an
function?

By freeing it.

Bmap.Free;

--
Rob


I've tryed to free it that way but the memory consumption is increasing for
each call.
The function is creating the Bitmap as the result value of the function and
the result parameter of the function must be stored somewere.

Dag



Back to top
Rob Kennedy
Guest





PostPosted: Fri Feb 06, 2004 6:04 pm    Post subject: Re: Release memory allocated by function result Reply with quote

DI Konsult wrote:
Quote:
I've tryed to free it that way but the memory consumption is increasing for
each call.

Let me guess: You're trying to use Task Manager as a debugging tool.
Don't. It reports how much memory the OS thinks your program is using,
but when you free something in Delphi, Delphi's memory manager does not
release that memory back to the OS, so Windows reports that your program
is using more memory than it really is. When possible, Delphi will
re-use that freed memory for other things.

Quote:
The function is creating the Bitmap as the result value of the function and
the result parameter of the function must be stored somewere.

Right. You're storing the result in the Bmap variable. And when you're
finished with it, you should free it. Just think of your
DoSomeThingBitmap function as though it were a constructor -- a factory.

--
Rob

Back to top
DI Konsult
Guest





PostPosted: Fri Feb 06, 2004 11:36 pm    Post subject: Re: Release memory allocated by function result Reply with quote


"Rob Kennedy" <me (AT) privacy (DOT) net> skrev i meddelandet
news:c00kue$11fiad$1 (AT) ID-220940 (DOT) news.uni-berlin.de...
Quote:
DI Konsult wrote:
I've tryed to free it that way but the memory consumption is increasing
for
each call.

Let me guess: You're trying to use Task Manager as a debugging tool.
Don't. It reports how much memory the OS thinks your program is using,
but when you free something in Delphi, Delphi's memory manager does not
release that memory back to the OS, so Windows reports that your program
is using more memory than it really is. When possible, Delphi will
re-use that freed memory for other things.

The function is creating the Bitmap as the result value of the function
and
the result parameter of the function must be stored somewere.

Right. You're storing the result in the Bmap variable. And when you're
finished with it, you should free it. Just think of your
DoSomeThingBitmap function as though it were a constructor -- a factory.

--
Rob

Hmmm, why do I then get the error message " Not enougt memory for this
operation" after a while ?

Dag



Back to top
DI Konsult
Guest





PostPosted: Sat Feb 07, 2004 12:06 am    Post subject: Re: Release memory allocated by function result Reply with quote


"Rob Kennedy" <me (AT) privacy (DOT) net> skrev i meddelandet
news:c00kue$11fiad$1 (AT) ID-220940 (DOT) news.uni-berlin.de...
Quote:
DI Konsult wrote:
I've tryed to free it that way but the memory consumption is increasing
for
each call.

Let me guess: You're trying to use Task Manager as a debugging tool.
Don't. It reports how much memory the OS thinks your program is using,
but when you free something in Delphi, Delphi's memory manager does not
release that memory back to the OS, so Windows reports that your program
is using more memory than it really is. When possible, Delphi will
re-use that freed memory for other things.

The function is creating the Bitmap as the result value of the function
and
the result parameter of the function must be stored somewere.

Right. You're storing the result in the Bmap variable. And when you're
finished with it, you should free it. Just think of your
DoSomeThingBitmap function as though it were a constructor -- a factory.

--
Rob

Sorry, youre right. My problem was as usual my self Smile
I was using this code
var
Img : TImage;

begin
Img := TImage.Create(self);
Img.Picture.BitMap := DoSomeThingWith Image(Image1.Picture.BitMap,0);
Image1.Picture.Bitmap.FreeImage;
Image1.Picture.Assign(Img.Picture.BitMap);
Img.Free;
end;

Using Img.Free did not free the memory.
Redeclaring Img

var
Img:TBitmap;
begin
Img := TBitMap.Create;
Img := DoSomeThingWith Image(Image1.Picture.BitMap,0);
Image1.Picture.Bitmap.FreeImage;
Image1.Picture.Assign(Img);
Img.Free;
end;


Solved the problem, naturally since the function created the Result with:
Result := TBitmap.Create;

Sorry for bothering you with this.

Dag



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.