[fpc-pascal] access violation?

Dmitry Boyarintsev skalogryz.lists at gmail.com
Fri Jul 27 18:16:54 CEST 2018


On Fri, Jul 27, 2018 at 11:15 AM, Ryan Joseph <ryan at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20180727/99e2d742/attachment.html>


More information about the fpc-pascal mailing list