Thread overview
[Issue 6436] New: Refcounted initialization bug
Aug 04, 2011
Andrej Mitrovic
Aug 04, 2011
Andrej Mitrovic
Apr 20, 2012
Andrej Mitrovic
Oct 26, 2012
Denis Shelomovskij
Oct 26, 2012
Denis Shelomovskij
Feb 05, 2013
Andrej Mitrovic
August 04, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6436

           Summary: Refcounted initialization bug
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrej.mitrovich@gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2011-08-04 13:35:13 PDT ---
import std.typecons;

struct Foo
{
    struct Payload
    {
        this(ref int bar, int value)
        {
            bar = value;
            assert(bar == 5);   // ok
        }
    }

    alias RefCounted!(Payload, RefCountedAutoInitialize.yes) Data;
    Data data;

    int bar;

    this(int value)
    {
        data = Data(bar, value);
        assert(bar == 5);  // throws, it's still 0
    }
}

void main()
{
    auto foo = Foo(5);
}

import std.typecons;

struct Foo
{
    struct Payload
    {
        this(ref int bar, int value)
        {
            bar = value;
            assert(bar == 5);   // ok..
        }
    }

    alias RefCounted!(Payload, RefCountedAutoInitialize.yes) Data;
    Data data;

    int bar;

    this(int value)
    {
        data = Data(bar, value);  // initializes bar to 5
        assert(bar == 5);  // throws, it's still 0
    }
}

void main()
{
    auto foo = Foo(5);
}

Foo's ctor is called, then Refcounted Payload's ctor is called which initializes Foo's integer field, but upon return to the Foo ctor the integer field seems to be reinitialized to 0.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 04, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6436



--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2011-08-04 13:36:01 PDT ---
Oh damnit duplicated code sorry:

import std.typecons;

struct Foo
{
    struct Payload
    {
        this(ref int bar, int value)
        {
            bar = value;
            assert(bar == 5);   // ok
        }
    }

    alias RefCounted!(Payload, RefCountedAutoInitialize.yes) Data;
    Data data;

    int bar;

    this(int value)
    {
        data = Data(bar, value);
        assert(bar == 5);  // throws, it's still 0
    }
}

void main()
{
    auto foo = Foo(5);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 20, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6436



--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-04-19 18:45:56 PDT ---
Interestingly if you pass by pointer instead of by ref it works fine. Here's a much more simplified example:

import std.typecons;
import std.stdio;

struct Struct1 {
    this(ref int val) { val = 5; }
}

struct Struct2 {
    this(int* val) { *val = 5; }
}

alias RefCounted!(Struct1) Str1;
alias RefCounted!(Struct2) Str2;

void main()
{
    Str1 str1;
    Str2 str2;
    int val;

    str1 = Str1(val);
    writeln(val);  // writes 0

    str2 = Str2(&val);
    writeln(val);  // writes 5
}

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


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |verylonglogin.reg@gmail.com


--- Comment #3 from Denis Shelomovskij <verylonglogin.reg@gmail.com> 2012-10-26 20:21:00 MSD ---
This issue is blocked by `emplace` issue which is fixed in pull 896: https://github.com/D-Programming-Language/phobos/pull/896

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



--- Comment #4 from Denis Shelomovskij <verylonglogin.reg@gmail.com> 2012-10-26 20:47:37 MSD ---
OK. Full fix added to the pull: https://github.com/D-Programming-Language/phobos/pull/896

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 12, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6436



--- Comment #5 from github-bugzilla@puremagic.com 2012-12-12 07:41:14 PST ---
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/63503202b74a5e97e7755847feac886db0bbe64f Fix `emplace` part of issue 6436

* Issue URL: http://d.puremagic.com/issues/show_bug.cgi?id=6436

https://github.com/D-Programming-Language/phobos/commit/dab3b2a7dda8459b5f609761a984811d3e7aec5d Fix Issue 6436 - Refcounted initialization bug

* Issue URL: http://d.puremagic.com/issues/show_bug.cgi?id=6436

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 05, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=6436


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED


--- Comment #6 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-02-05 13:19:58 PST ---
Thanks, fixed.

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