Class destructors - clarify what is safe

H. S. Teoh hsteoh at qfbox.info
Sun Feb 15 00:46:38 UTC 2026


On Sat, Feb 14, 2026 at 10:03:17PM +0000, Paul Backus via Digitalmars-d-learn wrote:
> On Saturday, 14 February 2026 at 18:42:21 UTC, H. S. Teoh wrote:
> > > 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.
> > 
> > Calling .destroy on a class object more than once may trigger UB.
> 
> This is technically true, in the sense that calling any function could
> conceivably trigger UB, but destructors *should* be idempotent, and
> any destructor marked as @safe *must* be idempotent.

True.  So any dtor that closes a file should set its handle to a null
value so that any subsequent calls will be a no-op.


--T


More information about the Digitalmars-d-learn mailing list