Potential crash when double-clicking buttons

Problem

When a button’s onClick event makes it cease to exist, double-clicking on it will crash.

I suspect that this is a bug, or at least if onClick is not supposed to delete the button being clicked, then an assertion for that would be helpful.

Repro

Add logic to WidgetsDemo to make the image button disappear on the second click on it:

ib->onClick = [=]()
{
    static int foo = 0;
    ++foo;
    if (foo == 2)
    {
        removeChildComponent (ib);
        components.removeObject (ib);
    }
};

Run the demo and double-click on the image button. Got the following crash in latest develop (57202b360ad)

Possible fix

Component::internalMouseUp could hold a weak reference to *this initialized before the call to the mouseUp method. If the component gets deleted in it then we should skip calling its mouseDoubleClick method.

5 Likes

Fix available in our JUCE branch

1 Like

I’m curious: what’s the hierarchy like at that point? It’s possible that I don’t fully understand how that area works anymore since so much has changed – but instead of adding the weak ref, why not move the bailing out check before the call to the double-click?

    // check for double-click
    if (me.getNumberOfClicks() >= 2)
    {
        if (checker.shouldBailOut())
            return;

        mouseDoubleClick (checker.eventWithNearestParent());

The crash happens at the call to mouseDoubleClick (being a virtual-call of a non-existing object it may jump to invalid memory) rather than at checker.shouldBailOut.
So apparently it means that checker.shouldBailOut() returns false otherwise it wouldn’t had crashed.

2 Likes

Thank you for reporting.

3 Likes

Sure! and also

for such a quick response :slight_smile:

1 Like

thx, this just fixed a double click issue for me too!

1 Like