@@ -718,57 +718,74 @@ $(GNAME Destructor):
718
718
$(D ~ this ( )) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(GLINK2 function, FunctionBody)
719
719
)
720
720
721
- $(P The destructor function is called when the object
722
- is deleted by the garbage collector, or when a
723
- $(DDSUBLINK spec/attribute, scope-class-var, `scope` class instance) goes out of scope.
724
- The syntax is:)
721
+ $(P The destructor function is called when:)
725
722
723
+ * A live object is deleted by the garbage collector
724
+ * A live $(DDSUBLINK spec/attribute, scope-class-var, `scope` class instance) goes out of scope
725
+ * $(REF1 destroy, object) is called on the object
726
+
727
+ $(P Example:)
728
+
729
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
726
730
------
731
+ import std.stdio;
732
+
727
733
class Foo
728
734
{
729
735
~this() // destructor for Foo
730
736
{
737
+ writeln("dtor");
731
738
}
732
739
}
733
- ------
734
740
735
- $(P There can be only one destructor declared per class, although
736
- other destructors $(DDSUBLINK spec/template-mixin, destructors, can be mixed in).
737
- A destructor does not have any parameters,
738
- and has no attributes. It is always virtual.
741
+ void main()
742
+ {
743
+ auto foo = new Foo;
744
+ destroy(foo);
745
+ writeln("end");
746
+ }
747
+ ------
739
748
)
740
749
741
- $(P The destructor is expected to release any resources held by the
750
+ * Only one destructor can be declared per class, although
751
+ other destructors $(DDSUBLINK spec/template-mixin, destructors, can be mixed in).
752
+ * A destructor does not have any parameters or attributes.
753
+ * A destructor is always virtual.
754
+
755
+ $(P The destructor is expected to release any non-GC resources held by the
742
756
object.
743
757
)
744
758
745
- $(P The program can explicitly inform the garbage collector that an
746
- object is no longer referred to with $(REF1 destroy, object), and
747
- then the garbage collector calls the destructor immediately. The
748
- destructor is guaranteed to never be called twice.
759
+ $(P The program can explicitly call the destructor of a live
760
+ object immediately with $(REF1 destroy, object).
761
+ The runtime marks the object so the destructor is never called twice.
749
762
)
750
763
751
- $(P The destructor for the super class automatically gets called when
752
- the destructor ends. There is no way to call the super destructor
764
+ $(P The destructor for the $(RELATIVE_LINK2 super_class, super class) automatically gets called when
765
+ the destructor ends. There is no way to call the super class destructor
753
766
explicitly.
754
767
)
755
768
756
- $(P The garbage collector is not guaranteed to run the destructor
757
- for all unreferenced objects. Furthermore, the order in which the
769
+ $(IMPLEMENTATION_DEFINED The garbage collector is not guaranteed to run the destructor
770
+ for all unreferenced objects.)
771
+
772
+ $(PANEL
773
+ $(DIVC spec-boxes, $(B Important:) The order in which the
758
774
garbage collector calls destructors for unreferenced objects
759
775
is not specified.
760
776
This means that
761
777
when the garbage collector calls a destructor for an object of a class
762
778
that has
763
779
members which are references to garbage collected objects, those
764
780
references may no longer be valid. This means that destructors
765
- cannot reference sub objects.
766
- This rule does not apply to auto objects or objects destructed
767
- with $(REF1 destroy, object), as the destructor is not being run
768
- by the garbage collector, meaning all references are valid.
781
+ cannot reference sub objects.)
782
+
783
+ $(NOTE This rule does not apply to a `scope` class instance or an object destructed
784
+ with `destroy`, as the destructor is not being run
785
+ during a garbage collection cycle, meaning all references are valid.)
769
786
)
770
787
771
- $(P Objects referenced from the data segment never get collected
788
+ $(P Objects referenced from the static data segment never get collected
772
789
by the GC.
773
790
)
774
791
0 commit comments