auto classes and finalizers
Georg Wrede
georg.wrede at nospam.org
Thu Apr 6 23:19:20 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.
The mere existence of a circular list that is not pointed-to from the
outside, is a programmer error. Unless one explicitly wants it to be
collected. But even then it's a programmer error if the items need
destructing, since the collection may or may not happen "ever".
So, in practice, whenever one wants to store items that need destructors
in a linked list, the list itself should be encapsulated in a class that
can guarantee the timely destruction of the items, as opposed to merely
abandoning them.
More information about the Digitalmars-d
mailing list