auto classes and finalizers

Don Clugston dac at nospam.com.au
Tue Apr 18 06:25:40 PDT 2006


Bruno Medeiros wrote:
> Don Clugston wrote:
>> kris wrote:
>>> Mike Capp wrote:
>>>> In article <e1cq0t$1fm5$1 at digitaldaemon.com>, kris says...
>>>>
>>>>> I thought the idea was that classes with dtors are /intended/ to be 
>>>>> explicitly cleaned up? That, implicit cleanup of resources (manana, 
>>>>> some time) was actually a negative aspect? At least, that's what 
>>>>> Mike was suggesting, and it seemed like a really good idea.
>>>>
>>>>
>>>> Um... can we avoid using "implicit" and "explicit" in this context? 
>>>> "Implicit"
>>>> to me means "without writing any code", which covers both RAII and 
>>>> GC cleanup
>>>> (if you're lucky). "Explicit" to me means manual calls to dtors or 
>>>> dispose(),
>>>> which is the worst of all possible approaches.
>>>
>>> Yeah, I see the murk. What would you prefer to call them? The 
>>> distinction being made there was whether the dtor was initiated via 
>>> delete/auto, versus normal collection by the GC (where the latter was 
>>> referred to as implicit).
>>
>> deterministic and non-deterministic.
> 
> I don't like those terms. Although they are not false (because 
> *currently* explicit destruction is deterministic, and implicit 
> destruction in non-deterministic), the fact of whether the destructor 
> was called deterministically or non-deterministically is not in itself 
> relevant to this issue. 

I'm not sure that this is correct, see below.

> What is relevant is the state of the object to 
> be destroyed (in defined or undefined state).

> Nor is implicit destruction/collection inherently non-deterministic and 
> vice-versa.  (even if systems that operated this way would be unpractical)

Yes, you're right, a finaliser could be invoked immediately whenever the 
last reference goes out of scope. But I think (not sure) that the issues 
with finalisers would disappear if they were deterministic in this 
manner. At least, I'm confident that non-deterministic scope-based 
destructors would suffer from the same problems that finalisers do.

> So far, I'm keeping the terms "implicit" and "explicit", as they seems 
> adequate to me and I don't find at all that RAII collection is 
> "implicit" or "without writing any code".

However, RAII has been contrasted with "explicit" memory management for 
a very long time. "Explicit" has a firmly established meaning of 'new' 
and 'delete', it's very confusing to use them to mean something entirely 
different. (If however, the distinction is between "gc" and "non-gc", 
let's call a spade a spade).

On this topic -- there's an interesting thread on comp.c++ by Andrei 
Alexandrescu about gc and RAII. Among other things, he argues that 
finalisers are a flawed concept that shouldn't be included. (BTW, he 
seems to be getting *very* interested in D -- he now has a link to the D 
spec on his website, for example -- so his opinions are worth examining).



More information about the Digitalmars-d mailing list