On Fri, Jul 27, 2018 at 11:15 AM, Ryan Joseph <r...@thealchemistguild.com>
wrote:

> I never use Free directly so I had no idea. How is that even accomplished
> in the language? I thought it was just a method and behaved accordingly.
>
> If it is magic then how do we call other methods on nil objects? There are
> times that would be handy if I could control it.


It's not a magic, it's explicit check for the value of "Self" within a
method.
Self would point to an actual instance of an object, if it's nil. Free
method won't do anything.

type
  TRobust = class(TObject)
  public
    v : Integer;
    procedure RobustMethod;
  end;

procedure TRobust.RobustMethod;
begin
  if Self = nil then Exit; // remove or comment out this line to start
getting AVs
  v := 5;
end;

var
  r : TRobust;
begin
  r := nil;
  r.RobustMethod;
end.

You could consider this a sanity check, similar to checking any other input
parameters.

thanks,
Dmitry
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to