--- Big Al wrote:
> Morning all.
> Yesterday day I spent a couple more wasted hours sorting out a
> problemo in my software which I eventually chased up to the
> function call not returning a bitmap:

Not wasted - you're learning stuff :-) Let me try and explain what's
happening in your code.

> function myapp.getaBM(oldBM:tbitmap):tbitmap;
> var bitmap:tbitmap
> begin
> bitmap:=tbitmap.create;

Translation: create a tbitmap-sized lump of memory, and point a
variable called 'bitmap' at it.

> //rest bitmap initialize here
> bitmap:=oldBM;

Translation: Now point the 'bitmap' variable at the same lump of memory
that 'oldBM' is pointing to.

> result:=bitmap;

Translation: Now point the 'result' variable at the same lump of memory
that 'bitmap' is pointing to. At this point 'bitmap', 'oldBM' and
'result' all point to the same lump of memory -- but it's *not* the
lump created by 'bitmap:=tbitmap.create' (nobody's pointing at it, but
it's still there -- you've created a memory leak)

> bitmap.free;    //gwan get lost result

Translation: Destroy the lump of memory that 'bitmap' is pointing at.
This is the same lump that 'oldBM' and 'result' are pointing at too, so
whenever you try to use *any* of those variables your program will go
bang, because that lump of memory no longer exists.

Your options are to either create the object outside the routine:

main
var EllePic : TBitmap;
begin
   EllePic := TBitmap.Create;
   createPictureOfElle( EllePic );
   EllePic.SaveToFile('c:\porn\elle.bmp');
   EllePic.Free;
end;

procedure createPictureOfElle( pic : TBitmap );
begin
   pic.whatever;
   pic.whatever;
end;

or you could have the routine create the object, and let the calling
routine .free it. I don't like this approach -- it means we have to
trust the calling routine.

main
var EllePic : TBitmap;
begin
   EllePic := createPictureOfElle();
   EllePic.SaveToFile('c:\porn\elle.bmp');
   EllePic.Free;
end;

procedure createPictureOfElle : TBitmap;
begin
   result := TBitmap.create;
   result.whatever;
   result.whatever;
end;

Hope this helps.

Cheers,
Kerry S

PS: Big Al, I just loved your role in 'South Park'. Any chance of an
autographed video?
_______________________________________________________________
DO YOU YAHOO!?
Get your free @yahoo.com.au address at http://mail.yahoo.com.au

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to