auto classes and finalizers

Georg Wrede georg at nospam.org
Thu Apr 6 15:14:43 PDT 2006


Lars Ivar Igesund wrote:
> Georg Wrede wrote:
>>kris wrote:
>>>Sean Kelly wrote:
>>>>kris wrote:
>>>>>Jarrett Billingsley wrote:
>>>>>
>>>>>> Hmm.  'auto' works well and good for classes whose references are
>>>>>> local variables, but .. what about objects whose lifetimes aren't
>>>>>> determined by the return of a function?
>>>>>>
>>>>>> I.e. the Node class is used only in LinkedList.  When a LinkedList
>>>>>> is killed, all its Nodes must die as well.  Since the Node
>>>>>> references are kept in the LinkedList and not as local variables,
>>>>>> there's no way to specify 'auto' for them.
>>>>>
>>>>> Heck, the LinkedList dtor /cannot/ rely on the nodes being valid if
>>>>> they are also managed by the GC :)
>>>>>
>>>>> So, as I understand it, one cannot legitimately execute that example.
>>>>
>>>> ...unless the LinkedList has a deterministic lifetime :-)
>>>
>>> <g> Touché !
>>
>> Hey, hey, hey...
>>
>> If anybody deletes stuff from a linked list, isn't it their
>> responsibility to fix the pointers of the previous and/or the next item,
>> to "bypass" that item??????
>>
>> The mere fact that no "outside" references exist to a particular item in
>> a linked list does _not_ make this item eligible for GC.
>>
>> Not in the current implementation, and I dare say, in no future
>> implementation ever.
>>
>> In other words, it is _guaranteed_ that _all_ items in a linked list are
>> valid.
> 
> Not if the linked list is circular (such that all items is linked to), but
> disjoint from the roots kept by the GC. This memory will be lost to a
> conservative GC, but can be detected some of the other types around.

If the linked list is circular, and at the same time there's no 
reference to this list from any GC examined area, then I'd consider this 
as a Programmer Fault.

Any set of "items", none of which is referenced from a "roots" area, is 
IMHO eligible for deletion. Whether this set is circular or not.

In other words, we should not strive to make the GC "too smart" for its 
own good. Either we see to it that items not wished for deletion are 
pointed to, or we accept that non-pointed-to items are considered passe.



More information about the Digitalmars-d mailing list