www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 15952] New: struct in closure is destroyed when parent

https://issues.dlang.org/show_bug.cgi?id=15952

          Issue ID: 15952
           Summary: struct in closure is destroyed when parent function
                    returns
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: ag0aep6g gmail.com

Found by Ramon who posted to D.learn:
http://forum.dlang.org/post/ygqsvhyjdmigeirtwice forum.dlang.org

----
struct S
{
    int x;
    ~this() {x = 1;}
}

void delegate() makeDelegate()
{
    auto s = S(2);
    return {assert(s.x == 2); /* fails; should pass */};
}

void main()
{
    auto dg = makeDelegate();
    dg();
}
----

Apparently, the destructor is called when makeDelegate returns. But s is in a
closure and should be destroyed only when that closure is collected.

--
Apr 23 2016