Found by Ramon who posted to D.learn:
http://forum.dlang.org/post/[email protected]
----
struct S
{
int x;
~this() {x = 1;}
}
void delegate() makeDelegate()
{
auto s = S(2);
return {assert(s.x == 2); /* fails; should pass */};
}
void main()
{
auto dg = makeDelegate();
dg();
}
----
Apparently, the destructor is called when makeDelegate returns. But s is in a closure and should be destroyed only when that closure is collected.
Comment #1 by snarwin+bugzilla — 2021-04-05T23:23:27Z
*** Issue 21669 has been marked as a duplicate of this issue. ***
Comment #2 by johan_forsberg_86 — 2021-04-06T05:22:08Z
Five year anniversary?
Comment #3 by schveiguy — 2021-04-06T17:07:18Z
As my issue got marked as a duplicate of this, I'd say the title and expectation is different from mine.
It would be a noble goal for the compiler to try and make this work, but I think the correct path is to disallow this delegate. Prior to 2.053, this was an error because the destructor would render the instance invalid. I think we should revert to that expectation -- a struct reference shnould not outlive its lifetime.
Comment #4 by ibuclaw — 2022-12-29T19:27:47Z
(In reply to Steven Schveighoffer from comment #3)
> As my issue got marked as a duplicate of this, I'd say the title and
> expectation is different from mine.
>
In both cases, there's a delegate function that references a field of the struct being destructor'd. Just two different ways to exit a scope to trigger the dtor call.