[Issue 14261] New: Struct destructors shouldn't be called when in a closure
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Sun Mar 8 00:29:24 PST 2015
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 at puremagic.com
Reporter: thecybershadow at 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.
--
More information about the Digitalmars-d-bugs
mailing list