Bug 24120 – Closures break constructor/destructor safety

Status
NEW
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2023-08-30T07:53:41Z
Last change time
2024-12-13T19:30:40Z
Assigned to
No Owner
Creator
FeepingCreature
Moved to GitHub: dmd#20327 →

Comments

Comment #0 by default_357-line — 2023-08-30T07:53:41Z
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.
Comment #1 by robert.schadek — 2024-12-13T19:30:40Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20327 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB