Think of a TBitmap as a pointer to a block of memory.

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

        // oldBM->memblock1, Bitmap->nil, Result->nil

bitmap:=tbitmap.create;

        // oldBM->memblock1, Bitmap->memblock2, Result->nil

//rest bitmap initialize here
bitmap:=oldBM;

        // oldBM->memblock1, Bitmap->memblock1, Result->nil, no pointers
to memblock2 so memblock2 is lost!

result:=bitmap;

        // oldBM->memblock1, Bitmap->memblock1, Result->memblock1

bitmap.free;    //gwan get lost result

        // oldBM->[freed], Bitmap->[freed], Result->[freed]

end;

So the bitmap that you passed to this function has been freed by it, and
the function will return this (already freed) bitmap.  Access violation
city.  And the function leaks memory to boot (a TBitmap is created and
promptly forgotten about)...

Cheers,

Carl Reynolds                      Ph: +64-9-4154790
CJN Technologies Ltd.             Fax: +64-9-4154791
[EMAIL PROTECTED]                DDI: +64-9-4154795
PO Box 302-278, North Harbour, Auckland, New Zealand
12 Piermark Drive, North Harbour Estate, Auckland, NZ
Visit our website at http://www.cjntech.co.nz/

> -----Original Message-----
> From: Alistair George [SMTP:[EMAIL PROTECTED]]
> Sent: Wednesday, July 28, 1999 7:11 AM
> To:   Multiple recipients of list delphi
> Subject:      [DUG]:  Any comments
> 
> 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:
> 
> function myapp.getaBM(oldBM:tbitmap):tbitmap;
> var bitmap:tbitmap
> begin
> bitmap:=tbitmap.create;
> //rest bitmap initialize here
> bitmap:=oldBM;
> result:=bitmap;
> bitmap.free;    //gwan get lost result
> end;
> 
> Now to me, the following quote from help seems to indicate the above
> code
> would be right:
> Any object instantiated by a call to Create should be destroyed by a
> call to
> Free, so that the object an be properly destroyed and the memory
> released.
> Free is successful even if the object is nil, so if the object was
> never
> initialized, for example, calling Free wont result in an error.
> 
> And the variable RESULT is always available for use. Seems RESULT is
> destroyed with the free, because the only way the code works is by
> using
> FreeOnRelease instead, which the bible does not recommend.
> 
> OR do I leave it up to the procedure/function to destroy all the items
> created within that area on exit, just like any other variable, thus
> alleviating the use of Free?
> Re the above I often see Try...except free xxxx  in called functions
> or
> procedures, written by others, who are much more adept than me.
> Cheers,
> Al+
> 
> ----------------------------------------------------------------------
> -----
>     New Zealand Delphi Users group - Delphi List -
> [EMAIL PROTECTED]
>                   Website: http://www.delphi.org.nz

application/ms-tnef

Reply via email to