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 

Re: Function optimisation...

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Graphics
View previous topic :: View next topic  
Author Message
Finn Tolderlund
Guest





PostPosted: Thu Jul 17, 2003 12:10 pm    Post subject: Re: Function optimisation... Reply with quote



"Bednarik Boldizsar" <beginner (AT) eunet (DOT) yu> skrev i en meddelelse
news:3f167aa1 (AT) newsgroups (DOT) borland.com...
Quote:
function DRAWSOMETHING(patam:integer):TBitmap;
bmp:=TBitmap.Create;
result:=bmp;
{ THE BMP.FREE COMMAND DOESN'D WORKS! }

Freeing the bitmap here is a disaster.
If you free the bitmap here then the result value will be pointing at
nothing.
When returning an object from a function you can't free it inside the
function.
You can only free it when you have returned from the function.

Quote:
should I use a procedure like this
procedure DRAWSOMETHING(destinationbmp:TBitmap;patam:integer);
begin
destination.canvas.draw...

That's what I would generally prefer.
But both should work.
--
Finn Tolderlund



Back to top
johnnie
Guest





PostPosted: Thu Jul 17, 2003 12:55 pm    Post subject: Re: Function optimisation... Reply with quote



Bednarik Boldizsar wrote:

Quote:
If I use a function in my component like this

function DRAWSOMETHING(patam:integer):TBitmap;
var
bmp:TBitmap;
begin
bmp:=TBitmap.Create;

{some operations with bmp}

result:=bmp;
{ THE BMP.FREE COMMAND DOESN'D WORKS! }
end;

then the bmp stays in memory...

how can i free it up, to optimize my code?

should I use a procedure like this

procedure DRAWSOMETHING(destinationbmp:TBitmap;patam:integer);
begin
destination.canvas.draw...
end;

instead of the previus function?

can somebody give me a suggestion?

For clarity it is recomended that a function should not
create and return any object so it is best to change the
function and pass from outside the bitmap where it should
draw something like

function DRAWSOMETHING(const aBmp:TBitmap; patam:integer):Integer
begin
Result := 0 // No error indication
try
{do something}
except
On e:Exception do begin
result:= -1 // an error occured.
end;
end;
end;

if you do not want to do it this way you can steel use
your approch but you need to make sure that the object
returned by the function is destroyed something like this

1)

Var
dum :TBitmap;
begin
dum := drawsomething(1);
{Do something with dum}
dum.free;


Or

with drawsomething(1) do
try
{do somehitng with the object}
finally
free;
end

Please keep in mind that this methods are best to be avoided
they are a maintanse nightmair for every one including you.

regards
johnnie.

Back to top
Bednarik Boldizsar
Guest





PostPosted: Thu Jul 17, 2003 2:04 pm    Post subject: Re: Function optimisation... Reply with quote



Thanx!

"Bednarik Boldizsar" <beginner (AT) eunet (DOT) yu> wrote

Quote:
If I use a function in my component like this

function DRAWSOMETHING(patam:integer):TBitmap;
var
bmp:TBitmap;
begin
bmp:=TBitmap.Create;

{some operations with bmp}

result:=bmp;
{ THE BMP.FREE COMMAND DOESN'D WORKS! }
end;

then the bmp stays in memory...

how can i free it up, to optimize my code?

should I use a procedure like this

procedure DRAWSOMETHING(destinationbmp:TBitmap;patam:integer);
begin
destination.canvas.draw...
end;

instead of the previus function?

can somebody give me a suggestion?





Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Graphics 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.