I went and looked at the assembly code for this - you are right - this 
will not cause an exception (I even tried it).

The main reason it works is that Delphi does not need a valid object 
pointer to call .Free - it is not a virtual method.

If Free were a virtual method, then a valid object pointer would be 
required to call free.

Doug

Totoliciu Denis Dan wrote:
> When I have posted that the following lines:
>
>   Obj := Nil;
>   Obj.Free();
>
> do not raise an exception, I didn't have Delphi at
> hand; but I knew that inside TObject.Free() it is
> written something like this:
>
> procedure TObject.Free();
> begin
>   if Self <> Nil then
>     Self.Destroy;
> end;
>
> The call to Free() through Obj (that is Nil) goes OK
> because:
> 1. Free() is not a virtual method; and as we all know,
> the binding is done at compile time. That means there
> is no need to access a VMT.
> 2. Every method of a class has a hidden parameter; and
>  that parameter is Self. When calling Free, there is
> no need that Obj is different then Nil as Obj (named
> Self inside the method Free()) is passed as parameter.
>
> Obj.Free() would raise an error when Obj points to a
> zone of memory that is not allowed to be accessed.
>   


Reply via email to