Do you need to check on Assigned before calling Free or FreeAndNil?



  • It's quite common in the code that this design is:

    MyObj: TObject;
    

    ..

    if Assigned(MyObj) then
    FreeAndNil(MyObj);

    or:

    if Assigned(MyObj) then
    MyObj.Free;

    The logic of the typewriter is probably like, "check the validity index before it's addressed."

    Question of whether verification is necessary if Assigned challenge FreeAndNil or Free?



  • No, it's not necessary.

    Procedure Assigned Not checking the validity of the indicator, as many people sometimes believe, it only checks the index. nil (sighs) http://docwiki.embarcadero.com/Libraries/Berlin/en/System.Assigned )

    Method Free already contains a verification nil (checked by reference TObject.Free)

    Procedure FreeAndNil Appropriation to the indicator nil It's a big deal. Freein which this verification is present (also checked by reference SysUtils.FreeAndNil)




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2