August 30, 2023
https://issues.dlang.org/show_bug.cgi?id=24120

          Issue ID: 24120
           Summary: Closures break constructor/destructor safety
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody@puremagic.com
          Reporter: default_357-line@yahoo.de

Consider this code:

@trusted:
extern(C) int printf(const char*, ...);

@safe:
struct S {
    int i;
    this(int i) { this.i = i; printf("  this(%i)\n", i); }
    ~this() { printf("  ~this(%i)\n", i); }
}

void delegate() @safe foo() {
    S s = S(5);
    return { printf("access s %i after destruction\n", s.i); };
}

void main() {
        foo()();
}

The closure in `foo` accesses `s` after the `s` destructor has run. This can be an issue if `S` frees resources in the destructor.

--