Can I remove an element from a global associative array from within a class destructor?

JN 666total at wp.pl
Sat Aug 3 09:42:31 UTC 2019


On Friday, 2 August 2019 at 23:13:10 UTC, realhet wrote:
> Today I read the documentation about structs, unions and 
> classes, but I haven't find any restrictions for the ~this() 
> destructors.
>
> Is there some extra rules regarding the GC and what I must not 
> do in the destructors?

Class destructors are similar to Java's finalizers in behavior. D 
doesn't give you a guarantee when the class destructor is called 
and it doesn't give you a guarantee if it will be called at all, 
so it doesn't really work for RAII-like resource management. If 
you want RAII-like behavior, either use structs (struct 
destructors are deterministic), or wrap your class inside a 
scoped/refCounted wrapper from std.typecons.


More information about the Digitalmars-d-learn mailing list