Acces Violation: assert with null instance

Bradley Smith digitalmars-com at baysmith.com
Wed Jan 24 23:00:16 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.
>>
>> Stewart.
> 
> It does check if it's null. That's how the access violation exception 
> gets thrown.

Perhaps this is a better example.

import std.stdio;
import std.asserterror;
class Class {}
void main() {
   Class c;
   try {
     version (alt) { assert(false); }
     assert(c);
   } catch (AssertError e) {
     writefln("An assert failed: ", e);
   }
}

C:\>dmd assertNull.d
c:\dmd\bin\..\..\dm\bin\link.exe assertNull,,,user32+kernel32/noi;

C:\>assertNull
Error: Access Violation

C:\>dmd assertNull.d -version=alt
c:\dmd\bin\..\..\dm\bin\link.exe assertNull,,,user32+kernel32/noi;

C:\>assertNull
An assert failed: AssertError Failure assertNull(7)



More information about the Digitalmars-d-learn mailing list