On Thu, 24 May 2012 13:47:31 -0400, Tove <t...@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!

-Steve

Reply via email to