www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 14261] New: Struct destructors shouldn't be called when in a


          Issue ID: 14261
           Summary: Struct destructors shouldn't be called when in a
           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

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();

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