Class destructors - clarify what is safe
Forum User
forumuser at example.com
Sun Feb 15 00:58:14 UTC 2026
On Saturday, 14 February 2026 at 18:42:21 UTC, H. S. Teoh wrote:
>> When entering a class destructor, are any of the dynamically
>> allocated class members safe to read?
>
> No. The order in which the GC deallocates objects is not
> defined, and when the dtor is run, any references to
> GC-allocated objects cannot be safely read, as those objects
> may have already been destroyed.
Does this deviate from the way C++ handles destruction?
https://stackoverflow.com/questions/20045904/c-can-i-use-this-safely-in-a-destructor
Quote:
" Can I use this in a destructor
Yes.
For example, I know I'm not supposed to do anything with base
classes, since they are gone.
No, the base classes are still intact at this point. Members (and
perhaps other base classes) of derived classes have already been
destroyed, but members and base classes of this class remain
until after the destructor has finished."
I thought that also in D all the members and the base objects of
the object which is destructed are still valid and accessible
until the destructor finished.
More information about the Digitalmars-d-learn
mailing list