Class destructors - clarify what is safe
Brother Bill
brotherbill at mail.com
Sat Feb 14 17:36:32 UTC 2026
On page 325 of Programming in D, we may have a single
parameterless destructor named ```~this()```.
A class may have members of string, File, other classes, etc.
When entering a class destructor, are any of the dynamically
allocated class members safe to read? That is, can we expect
that a class instance entering its destructor is "fully intact",
satisfying all its invariants, and all member variables have not
undergone any destruction at this point?
I would expect that a class with strings, FILE handles, etc.
would need these to do its housekeeping. If we opened a file in
the constructor, we should be able to close the file in the
destructor.
As we can explicitly call destroy() on a class instance multiple
times, do we need to be "careful" of only closing a file once,
rather than closing a file multiple times, if that can cause
issues.
What guarantees, if any, exist on entering a class destructor?
More information about the Digitalmars-d-learn
mailing list