In Delphi documentation is specified to not call
Destroy directly. You need to use Free who call
Destroy and verify that Object is not Nil. 
Adrian


--- Carl Jacobs <[EMAIL PROTECTED]> a écrit :


---------------------------------

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]



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

  
    
---------------------------------
  YAHOO! GROUPS LINKS

  
    Visit your group "delphi-en" on the web.
   
    To unsubscribe from this group, send an email to:
 [EMAIL PROTECTED]
   
    Your use of Yahoo! Groups is subject to the Yahoo!
Terms of Service.

  
---------------------------------




Cordialement, 
Adrian BUNYI



        

        
                
___________________________________________________________________________ 
Nouveau : téléphonez moins cher avec Yahoo! Messenger ! Découvez les tarifs 
exceptionnels pour appeler la France et l'international.
Téléchargez sur http://fr.messenger.yahoo.com


------------------------ Yahoo! Groups Sponsor --------------------~--> 
Fair play? Video games influencing politics. Click and talk back!
http://us.click.yahoo.com/2jUsvC/tzNLAA/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