alias this and null reference

Simen Kjaeraas simen.kjaras at gmail.com
Fri Apr 27 10:08:42 PDT 2012


On Friday, 27 April 2012 at 16:57:52 UTC, Namespace wrote:
> On Friday, 27 April 2012 at 16:49:49 UTC, Simen Kjaeraas wrote:
>> On Friday, 27 April 2012 at 16:36:04 UTC, Namespace wrote:
>>> By the following code i get a normal Access Violation.
>>> My question is: why? Even if "f0" is null, the object must be
>>> converted to Ref and there i check if the given object is 
>>> null.
>>
>> When trying to convert f0 to Ref, the compiler has to look in 
>> the
>> vtable for the getRef function. The vtable is in a nonsensical
>> place when the class reference is null, and thus it bails out.
>>
>> Solution: mark getRef as final.
>
> Great, thank you for reply, it works :).
> It is maybe possible, that I get another error output as "null 
> this"?
> Maybe an assert or something else?

Now we're delving into some deep magic of the compiler. In 
non-release builds, the compiler adds a pre-function invariant, 
which is called before the function is called. This invariant 
contains the equivalent of assert(this !is null, "null this");, 
and this is what you run into.

There's currently no way to get rid of this but to compile for 
release, in which case your own assert also goes out the window.



More information about the Digitalmars-d-learn mailing list