[Issue 15341] segfault with std.signals slots

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sun Jun 19 14:21:47 PDT 2016


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

hbaelx at hotmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|nobody at puremagic.com        |hbaelx at hotmail.com

--- Comment #2 from hbaelx at hotmail.com ---
The problem was that disconnect calls rt_detachDisposeEvent for that object
instance regardless of whether there were other connected slots from that
instance, thus unhook would not get called so long as a single slot from that
object was disconnected. My proposed solution:

Original
===========
final void disconnect(slot_t slot)
    {
        debug (signal) writefln("Signal.disconnect(slot)");
        for (size_t i = 0; i < slots_idx; )
        {
            if (slots[i] == slot)
            {   slots_idx--;
                slots[i] = slots[slots_idx];
                slots[slots_idx] = null;        // not strictly necessary

                Object o = _d_toObject(slot.ptr);
                rt_detachDisposeEvent(o, &unhook);
            }
            else
                i++;
        }
}
===========

Solution
==========
final void disconnect(slot_t slot)
{
    size_t disconnectedSlots = 0;
    size_t instancePreviousSlots = 0;

    for (size_t i = 0; i < slots_idx; )
    {
        if(slots[i].ptr is  slot.ptr)
        instancePreviousSlots++;

        if (slots[i] == slot)
        {   
            slots_idx--;
            disconnectedSlots++;
            slots[i] = slots[slots_idx];
            slots[slots_idx] = null;        // not strictly necessary
        }
        else
            i++;
    }

    if(instancePreviousSlots == disconnectedSlots)
    {
      Object o = _d_toObject(slot.ptr);
      rt_detachDisposeEvent(o, &unhook);
    }
}
========

Submitting a PR in Github, will update status as soon as it is accepted.

--


More information about the Digitalmars-d-bugs mailing list