 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
DI Konsult Guest
|
Posted: Fri Feb 06, 2004 5:16 pm Post subject: Release memory allocated by function result |
|
|
Following code:
function DoSomeThingBitmap(Bitmap: TBitmap): TBitmap;
begin
....
Result := TBitmap.Create;
....
end;
var
Bmap:TBitMap;
begin
|
|
| Back to top |
|
 |
Bruce Roberts Guest
|
Posted: Fri Feb 06, 2004 5:34 pm Post subject: Re: Release memory allocated by function result |
|
|
"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
|
Posted: Fri Feb 06, 2004 5:37 pm Post subject: Re: Release memory allocated by function result |
|
|
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
|
Posted: Fri Feb 06, 2004 5:43 pm Post subject: Re: Release memory allocated by function result |
|
|
"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
|
Posted: Fri Feb 06, 2004 6:04 pm Post subject: Re: Release memory allocated by function result |
|
|
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
|
Posted: Fri Feb 06, 2004 11:36 pm Post subject: Re: Release memory allocated by function result |
|
|
"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
|
Posted: Sat Feb 07, 2004 12:06 am Post subject: Re: Release memory allocated by function result |
|
|
"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
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 |
|
 |
|
|
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
|
|