On 2/14/26 4:46 PM, H. S. Teoh wrote:
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
The way I've learned to see it for decades now, initially from the point
of view of C++, calling the destructor more than once should be a mistake.
The reason is, when the destructor completes, the object is considered
to be dead. Calling any non-static function on it should be considered a
programming error.
I know structs are different because they are set to their initial state
after certain operations (e.g. I think destroy()) but in that case the
object is still an object, just morphed to its .init value.
What do others think?
Ali