Suggestion: "fix" assert(obj)

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Thu Jun 14 03:02:32 PDT 2007


Xinok wrote:
> Kristian Kilpi wrote:
>> So, one should usually write instead:
>>
>>   assert(obj !is null);
>>   assert(obj);
> 
> Until we do get a fix for this problem, wouldn't it be easiest to put 
> this check in the invariant itself?
> 
> invariant{
>     assert(this !is null);
> }

It would be, except that's too late. The code that gets called on 
assert(obj) goes like this:
---
void _d_invariant(Object o)
{   ClassInfo c;

     //printf("__d_invariant(%p)\n", o);

     // BUG: needs to be filename/line of caller, not library routine
     assert(o !is null); // just do null check, not invariant check

     c = o.classinfo;
     do
     {
         if (c.classInvariant)
         {
             (*c.classInvariant)(o);
         }
         c = c.base;
     } while (c);
}
---
(Note: The assert is usually not compiled-in since this code is in 
Phobos and the distributed binary version is compiled with -release :( )

The actual invariant code is called in the innermost nested block. The 
last statement before the loop accesses the vtable pointer of 'o', which 
segfaults/causes an access violation if "o is null". Before the 
invariant even gets a chance to run...



More information about the Digitalmars-d mailing list