digitalmars.D.bugs - [Issue 14261] New: Struct destructors shouldn't be called when in a
- via Digitalmars-d-bugs (54/54) Mar 08 2015 https://issues.dlang.org/show_bug.cgi?id=14261
https://issues.dlang.org/show_bug.cgi?id=14261 Issue ID: 14261 Summary: Struct destructors shouldn't be called when in a closure Product: D Version: D2 Hardware: All OS: All Status: NEW Severity: major Priority: P1 Component: DMD Assignee: nobody puremagic.com Reporter: thecybershadow gmail.com Illustrative example: ////////////// example.d ///////////// import std.stdio; auto getLogger() { auto f = File("log.txt", "w"); return (string s) => f.writeln(s); } void main() { auto logger = getLogger(); logger("Hello, world!"); } ////////////////////////////////////// This will crash because the file is already destroyed by the time the lambda runs. Test case: ///////////////// test.d //////////////// struct S { int i; ~this() { i--; } } auto outerFun() { auto s = S(1); void innerFun() { assert(s.i == 1); } return &innerFun; } void main() { auto f = outerFun(); f(); } ///////////////////////////////////////// Now that we have finalization of structs on the heap, maybe it's time to move closure destruction from end of scope to GC-collection time. I'm not sure if accessing destroyed objects can be considered a safety problem, but if it is, all the more reason to fix. --
Mar 08 2015