Re: When is a class's destructor called?
On Sunday, 29 December 2013 at 10:16:33 UTC, ponce wrote: If you want deterministic destruction, you have plenty of choices: - scoped! - using structs instead - scope(exit) - RefCounted! - Unique! - ... Do you happen to know of any clever technique to make object, and everything that inherits from it, a referenced counted object?
Re: When is a class's destructor called?
Mike: How does the garbage collector know when something has gone out of scope The garbage collector doesn't know that. Look in Wikipedia how a Mark&Sweep GC works. Bye, bearophile
Re: When is a class's destructor called?
On 12/29/2013 04:00 AM, Mike wrote: On Sunday, 29 December 2013 at 10:16:33 UTC, ponce wrote: If you want deterministic destruction, you have plenty of choices: - scoped! - using structs instead - scope(exit) - RefCounted! - Unique! - ... Do you happen to know of any clever technique to make object, and everything that inherits from it, a referenced counted object? There is std.typecons.RefCounted: http://dlang.org/phobos/std_typecons.html#.RefCounted Ali
Re: When is a class's destructor called?
Ok, I understand that, but since I don't have a garbage collector, what must I implement to call the destructor when it goes out of scope? Another way of asking the question is: How does the garbage collector know when something has gone out of scope and is therefore safe to collect? And where is that implemented in the runtime? If you want deterministic destruction, you have plenty of choices: - scoped! - using structs instead - scope(exit) - RefCounted! - Unique! - ... Yet using classes for ressources is not composable. It's either: - using classes for the convenience and deal with resource release manually - using structs It's an open quesiton for me which choice is best.
Re: When is a class's destructor called?
Also destructors are not guaranteed to be run by D specification. It is perfectly legal to skip such cleanup step upon program termination. http://dlang.org/class.html#destructors : "The garbage collector is not guaranteed to run the destructor for all unreferenced objects. Furthermore, the order in which the garbage collector calls destructors for unreference objects is not specified. This means that when the garbage collector calls a destructor for an object of a class that has members that are references to garbage collected objects, those references may no longer be valid. This means that destructors cannot reference sub objects. 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."
Re: When is a class's destructor called?
On Sunday, 29 December 2013 at 06:53:08 UTC, Mike Parker wrote: On 12/29/2013 2:03 PM, Mike wrote: x's destructor never gets called. What do I need to implement to have the destructor called when x goes out of scope? Class destructors are not called when an object goes out of scope. They are called by the garbage collector only when memory needs to be reclaimed or during the cleanup cycle during app shutdown. Ok, I understand that, but since I don't have a garbage collector, what must I implement to call the destructor when it goes out of scope? Another way of asking the question is: How does the garbage collector know when something has gone out of scope and is therefore safe to collect? And where is that implemented in the runtime?
Re: When is a class's destructor called?
On Sunday, 29 December 2013 at 06:58:03 UTC, Mike wrote: Ok, I understand that, but since I don't have a garbage collector, what must I implement to call the destructor when it goes out of scope? Patch compiler to make all classes scope classes. But that won't be D anymore.
Re: When is a class's destructor called?
Ali Çehreli: On 12/29/2013 04:00 AM, Mike: Do you happen to know of any clever technique to make object, and everything that inherits from it, a referenced counted object? There is std.typecons.RefCounted: http://dlang.org/phobos/std_typecons.html#.RefCounted I think it only works on a single object, not on the whole tree. (And I think RefCounted is not used often in D.) Bye, bearophile
Re: When is a class's destructor called?
On 12/29/2013 2:03 PM, Mike wrote: x's destructor never gets called. What do I need to implement to have the destructor called when x goes out of scope? Class destructors are not called when an object goes out of scope. They are called by the garbage collector only when memory needs to be reclaimed or during the cleanup cycle during app shutdown.
Re: When is a class's destructor called?
On Sunday, 29 December 2013 at 14:24:05 UTC, bearophile wrote: Ali Çehreli: On 12/29/2013 04:00 AM, Mike: Do you happen to know of any clever technique to make object, and everything that inherits from it, a referenced counted object? There is std.typecons.RefCounted: http://dlang.org/phobos/std_typecons.html#.RefCounted I think it only works on a single object, not on the whole tree. (And I think RefCounted is not used often in D.) Bye, bearophile Yep, and it's a struct, so no inheritance.