www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16249] New: std.signals: disconnect() is unsafe during emit()

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

          Issue ID: 16249
           Summary: std.signals: disconnect() is unsafe during emit()
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: nmtigor.wang googlemail.com

Hello,

conisder the following code:

============================
module dtest;

import std.signals;

struct SIGLine
{
    myLINE line;
    int value;
}

class myLINE
{
    mixin Signal!SIGLine;

    void value(int v)
    {
        if (v >= 0)
            emit(SIGLine(this, v));
        else
            emit(SIGLine(new myLINE, v));
    }
}

class Dot
{
    int value;

    myLINE line_;
    void line(myLINE line_x)
    {
        if (line_ is line_x)
            return;

        if (line_ !is null)
        {
            line_.disconnect(&watch);
        }
        line_ = line_x;
        line_.connect(&watch);
    }

    void watch(SIGLine sigline)
    {
        line = sigline.line;
        value = sigline.value;
    }
}

 system unittest
{
    auto dot1 = new Dot;
    auto dot2 = new Dot;
    auto line = new myLINE;
    dot1.line = line;
    dot2.line = line;

    line.value = 11;
    assert(dot1.value == 11);
    assert(dot2.value == 11);

    line.value = -22;
    assert(dot1.value == -22);
    assert(dot2.value == -22); // ERROR
}
============================

The error is because slots_idx is changed during the loop in emit().

--
Jul 07 2016