Acces Violation: assert with null instance

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Wed Jan 24 15:45:20 PST 2007


Walter Bright wrote:
> Stewart Gordon wrote:
>> Lionello Lunesu wrote:
>>> I really think this used to work (like in C++) :
>>>
>>> #class Class {}
>>> #void main(){
>>> #    Class c;
>>> #    assert(c);
>>> #}
>>>
>>> With 1.0, I get an access violation in 
>>> _D9invariant12_d_invariantFC6ObjectZv, but why?
>> <snip>
>>
>> For some strange reason, assert on an object reference checks that the 
>> invariants are satisfied instead of that the reference isn't null. 
>> There's nothing to this effect in the spec, so I don't know how it 
>> came about.  While it may be useful, it certainly shouldn't do it 
>> _instead of_ checking it isn't null.
> 
> It does check if it's null. That's how the access violation exception 
> gets thrown.

Access violations are hardware-generated, and not a particularly nice 
way to detect an error. Especially since no usable file & line 
information is generated.

The invariant checking code also contains "assert(o !is null)" before it 
does anything else, but that code is in Phobos. And the Phobos libs 
distributed with DMD seem to be compiled with '-release', stripping that 
code.
And even if it wasn't, the file & line displayed on failure would be 
Phobos' internal/invariant.d:14 or similar instead of the place the user 
put an assert(obj).


More information about the Digitalmars-d-learn mailing list