[Issue 24120] New: Closures break constructor/destructor safety

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Aug 30 07:53:41 UTC 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 at puremagic.com
          Reporter: default_357-line at 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.

--


More information about the Digitalmars-d-bugs mailing list