Programming in D p288. Destructor called too many times.
user1234
user1234 at 12.de
Mon Feb 9 21:58:14 UTC 2026
On Monday, 9 February 2026 at 20:19:26 UTC, Brother Bill wrote:
> The destructor runs too many times. I would expect it to run
> just once.
> What am I doing "wrong"?
I'd suget a simpler example:
```d
extern(C) int rand();
static cnt = 1;
struct S
{
~this()
{
cnt--;
}
}
void main()
{
const cond = ((rand() % 8) + 1) != 0; // not foldable but you
know it's true
if (cond)
{
S s;
} // scope leave, run S
dtor
assert(cnt == 0);
}
```
More information about the Digitalmars-d-learn
mailing list