D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21669 - closure over type with destructor allows accessing destroyed value if used after scope exits
Summary: closure over type with destructor allows accessing destroyed value if used af...
Status: RESOLVED DUPLICATE of issue 15952
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-02-28 05:34 UTC by Steven Schveighoffer
Modified: 2021-04-05 23:23 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Steven Schveighoffer 2021-02-28 05:34:34 UTC
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 Paul Backus 2021-04-05 23:23:27 UTC

*** This issue has been marked as a duplicate of issue 15952 ***