----- Original Message ----- 
Subject: [delphi-en] FreeAndNil should be called NilAndFree>
> Well who would have believed that FreeAndNil has a bug in it!
>
> The following code shows the essence of the problem.
>
> type
>  TA = Class
>    destructor Destroy;
>  end;
>
>  TB = Class
>    destructor Destroy;
>    procedure SomeMethod;
>  end;
>
> var
>  A : TA;
>  B : TB;
>
> destructor TB.Destroy;
> begin
>  FreeAndNil(A);
>  inherited;
> end;
>
> procedure TB.SomeMethod;
> begin
>  { Some code that references other fields/methods in TB - The exception 
> occurs at this point };
> end;
>
> destructor TA.Destroy;
> begin
>  B.SomeMethod;   { If using FreeAndNil, then B is already nil at this 
> point. SomeMethod is still called, but with a self of 0 }
>  inherited;
> end;
>
> begin
>  A : TA.Create;
>  B : TB.Create;
>
>  FreeAndNil(B);    <- This fails in TB.SomeMethod. Because B is set to nil 
> before it's freed.
>
>  B.Free;        <- This works.
>  B := nil;
> end;
>
> If you check the VCL source code (in Delphi 7 at least) then it really 
> does Nil before it Frees.
You have forgotten to put override on the Destructor so Free is not 
correctly freeing up memory.

David Bolton




------------------------ Yahoo! Groups Sponsor --------------------~--> 
Get Bzzzy! (real tools to help you find a job). Welcome to the Sweet Life.
http://us.click.yahoo.com/KIlPFB/vlQLAA/TtwFAA/i7folB/TM
--------------------------------------------------------------------~-> 

-----------------------------------------------------
Home page: http://groups.yahoo.com/group/delphi-en/
To unsubscribe: [EMAIL PROTECTED] 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/delphi-en/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply via email to