Destructor nonsense on dlang.org

Alex Rønne Petersen alex at lycus.org
Thu May 24 10:55:02 PDT 2012


On 24-05-2012 19:47, Tove wrote:
> On Thursday, 24 May 2012 at 17:06:19 UTC, ponce wrote:
>> I really had a hard time to believe it when #D told me so, but there
>> is no guaranteed order of destruction and as you cannot relies on
>> members still being alive in a class destructor.
>> All of it can happen when making absolutely no cycles in the object
>> graph.
>>
>> What I do now is having a close function for each class which hold a
>> non-memory resource.
>>
>> This is writtent in TDPL, but I wish I was told earlier :)
>
> http://dlang.org/class.html#destructors
>
> "This rule does not apply to auto objects or objects deleted with the
> DeleteExpression, as the destructor is not being run by the garbage
> collector, meaning all references are valid."
>
> i.e. non gc resources are fine... and it's also fine if you call
> clear()... it's only a problem if you rely on automatic collection and
> reference a object... so there's no need for close, as clear() will do
> the trick.
>
>

I would strongly advise against that, because a missed clear() means 
your finalizer may be run by the runtime's finalization machinery, and 
thus invalidate any invariants you were relying on in the finalizer.

-- 
Alex Rønne Petersen
alex at lycus.org
http://lycus.org


More information about the Digitalmars-d mailing list