Bug 21669 – closure over type with destructor allows accessing destroyed value if used after scope exits
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-02-28T05:34:34Z
Last change time
2021-04-05T23:23:27Z
Assigned to
No Owner
Creator
Steven Schveighoffer
Comments
Comment #0 by schveiguy — 2021-02-28T05:34:34Z
An example:
import std.stdio;
struct S
{
int i;
~this() { i = -1;}
}
void main()
{
int delegate() dg;
{
S s;
dg = () { return s.i; };
writeln(dg()); // 0
}
writeln(dg()); // -1
}
Allowing access to a destroyed variable where a destructor has invalidated the object should not be allowed. Prior to 2.053, this was an error. According to Paul Backus, the responsible PR was this: https://github.com/dlang/dmd/pull/5292
One who has created a delegate would probably expect that when calling the delegate at a later time, the state of the object should be preserved, and not destroyed. For some cases, it might be dangerous safety-wise to use an object after destruction.
Fixing this issue probably requires a long deprecation period, as no doubt code that depends on this (or works in spite of the dangers) exists today.
Comment #1 by snarwin+bugzilla — 2021-04-05T23:23:27Z
*** This issue has been marked as a duplicate of issue 15952 ***