www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6167] New: RefCounted and lazy/delegate

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6167

           Summary: RefCounted and lazy/delegate
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: jsancio gmail.com



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!

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 16 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6167




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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 16 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6167


Denis Shelomovskij <verylonglogin.reg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |verylonglogin.reg gmail.com
         Resolution|                            |DUPLICATE



15:45:47 MSD ---
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 ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 26 2012