Invariant for default construction

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Sat Dec 20 19:11:05 PST 2014


On 12/20/14 7:16 PM, Walter Bright wrote:
> On 11/17/2014 11:58 PM, Rainer Schuetze wrote:
>> I remember having an invariant on a tree structure checking
>> consistency by
>> verifying the children and parent references. This crashed when adding a
>> destructor. With the proposed change it will always crash.
>>
>> The problem is that the destructors of the tree nodes are called in
>> arbitrary
>> order when they are collected by the GC. Class instances are also made
>> invalid
>> after calling the destructor (the vtbl is zeroed).
>>
>> I wonder if
>>
>> - such invariants are invalid,
>> - the GC should bypass the invariant when calling the destructor
>> - or we should never call the invariant with the destructor?
>
> Invariants should be checking the state of the object that it owns, not
> other objects. I would consider such an invariant invalid.

Wouldn't a tree own its nodes? I find the idea of a tree checking its 
nodes to ensure it's properly sorted (or maybe properly balanced) cannot 
possibly be done without actually looking at its nodes.

How do you propose one would check that invariant?

-Steve


More information about the Digitalmars-d mailing list