Bug 14261 – Struct destructors shouldn't be called when in a closure

Status
NEW
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-03-08T08:29:24Z
Last change time
2024-12-13T18:41:05Z
Assigned to
No Owner
Creator
Vladimir Panteleev
Moved to GitHub: dmd#18955 →

Comments

Comment #0 by dlang-bugzilla — 2015-03-08T08:29:24Z
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.
Comment #1 by robert.schadek — 2024-12-13T18:41:05Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18955 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB