April 27, 2012 Re: alias this and null reference | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas | On Friday, April 27, 2012 18:49:48 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.
How about just sticking an assertion in with_ref to verify that it's parameter is non-null?
- Jonathan M Davis
|
April 27, 2012 Re: alias this and null reference | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Friday, 27 April 2012 at 18:19:33 UTC, Jonathan M Davis wrote:
> On Friday, April 27, 2012 18:49:48 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.
>
> How about just sticking an assertion in with_ref to verify that it's parameter
> is non-null?
>
> - Jonathan M Davis
I write my Ref struct, and Adam Ruppe wrote his NotNull struct, to avoid exactly this.
It's annoying to write "assert(param !is null);" every time again; a simple keyword or Ref!(Type) instead of Type is much better. But this is my opinion.
That is, how I handle the "null this" assertion now:
mixin template TRef(T) {
final Ref!(T) getRef() in {
assert(this !is null, "Object is null!");
} body {
return Ref!(T)(this);
}
alias getRef this;
}
Work fine so far.
|
April 27, 2012 Re: alias this and null reference | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Friday, 27 April 2012 at 17:47:48 UTC, Jonathan M Davis wrote:
> Access violations / segfaults aren't exceptions. They're OS signals. You can't catch them.
Actually, on Windows, access violation is a thrown Error.
I don't think you should catch it, but you can.
See druntime's src/rt/deh.d for the implementation.
On Linux though, you can't catch the signal.
|
Copyright © 1999-2021 by the D Language Foundation