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.









[Non-text portions of this message have been removed]



------------------------ Yahoo! Groups Sponsor --------------------~--> 
1.2 million kids a year are victims of human trafficking. Stop slavery.
http://us.click.yahoo.com/.QUssC/izNLAA/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