Destructor nonsense on dlang.org
Regan Heath
regan at netmail.co.nz
Fri May 25 02:21:19 PDT 2012
On Thu, 24 May 2012 18:57:11 +0100, Steven Schveighoffer
<schveiguy at yahoo.com> wrote:
> On Thu, 24 May 2012 13:47:31 -0400, Tove <tove at fransson.se> 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.
>
> There's a big problem with this though. Your destructor *has no idea*
> whether it's being called from within a collection cycle, or from
> clear. You must assume the most restrictive environment, i.e. that the
> dtor is being called from the GC.
>
> This is even true with struct dtors!
The C# dispose model suggests/gives examples of handling this sort of
problem using a bool and 2 dispose methods, see:
http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx
Could we do something similar in D, i.e. provide a template class which
could wrap any reference and implement this..
R
--
Using Opera's revolutionary email client: http://www.opera.com/mail/
More information about the Digitalmars-d
mailing list