Bug 6167 – RefCounted and lazy/delegate

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2011-06-16T15:57:00Z
Last change time
2012-10-26T04:45:47Z
Assigned to
nobody
Creator
jsancio

Comments

Comment #0 by jsancio — 2011-06-16T15:57:31Z
I am not sure what is going but this maybe a dmd bug so filing here. The following program: import std.typecons; import std.exception; struct Struct { this(int dummy) { refCount = RefCounted!Impl(Impl(dummy)); } ~this() {} RefCounted!Impl refCount; struct Impl { int dummy; } Struct fun(bool now) { if(now) throw new Exception(""); return Struct(1); } } void lazyFun(lazy Struct exp) { try { exp(); } catch(Exception e) {} } void delegateFun(Struct delegate() exp) { try { exp(); } catch(Exception e) {} } void main() { auto var = Struct(1); try { auto result = var.fun(true); } catch(Exception e) {} delegateFun({ return var.fun(true); }); // segfaults lazyFun(var.fun(true)); // segfaults assertThrown(var.fun(true)); // segfaults } Produces the following nonsensical output: _RefCounted@95F9410: initialized with (Impl _param_0) RefCounted!(Impl)@B74B8E40: decrement refcount to 157258767 RefCounted!(Impl)@B74B8E40: decrement refcount to 157258766 RefCounted!(Impl)@95F940E: decrement refcount to 65535 Notice the value of the refcount!
Comment #1 by jsancio — 2011-06-16T16:00:39Z
Also, note that if change fun to not be a member function you get the following: struct Struct { this(int dummy) { refCount = RefCounted!Impl(Impl(dummy)); } ~this() {} RefCounted!Impl refCount; struct Impl { int dummy; } } Struct fun() { throw new Exception(""); } //... $ ../dmd/dmd/src/dmd -debug=RefCounted -w -gc ref_test.d ../dmd/phobos/std/typecons.d && ./ref_test _RefCounted@89A8410: initialized with (Impl _param_0) RefCounted!(Impl)@89A8410: freeing... done! Which is the expected result.
Comment #2 by lovelydear — 2012-04-24T11:27:52Z
This program runs but produces no output on 2.059. I don't know what compilation flags were used.
Comment #3 by verylonglogin.reg — 2012-10-26T04:45:47Z
Reduced testcase: --- import std.stdio; struct Struct { int dummy, count = 0; this(int dummy) { writefln(" this(dummy = %s): count: %s", this.dummy = dummy, ++count); } ~this() { writefln("~this(dummy = %s): count: %s", dummy, --count); } static Struct getWithDummy2() { return Struct(2); } } static Struct neverCalled() { assert(0); } void lazyFun(lazy Struct exp) { } void main() { Struct var = Struct(1); (){ return var.getWithDummy2(); }(); lazyFun(neverCalled()); } --- Output: --- this(dummy = 1): count: 1 this(dummy = 2): count: 1 ~this(dummy = 2): count: 0 ~this(dummy = 10366848): count: 4203019 ~this(dummy = 1): count: 0 --- So this is a duplicate of Issue 8182 which is fixed in dmd 2.060. *** This issue has been marked as a duplicate of issue 8182 ***