| View previous topic :: View next topic |
| Author |
Message |
Finn Tolderlund Guest
|
Posted: Thu Jul 17, 2003 12:10 pm Post subject: Re: Function optimisation... |
|
|
"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
|
Posted: Thu Jul 17, 2003 12:55 pm Post subject: Re: Function optimisation... |
|
|
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
|
Posted: Thu Jul 17, 2003 2:04 pm Post subject: Re: Function optimisation... |
|
|
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 |
|
 |
|