Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot move or copy instance of a class exposing Slot without breaking callback #8

Open
michaelcowan opened this issue Aug 25, 2019 · 1 comment
Labels
bug Something isn't working

Comments

@michaelcowan
Copy link
Owner

michaelcowan commented Aug 25, 2019

The intended use of ass for class decoupling will not invoke the callback of the expected instance when copied or moved.

e.g.

class Popup {
public:
    Slot<> show = Slot<>(this, &Popup::render);
private:
    void render() {
        Popup * pointer = this;
    }
};

Popup popup;
Signal<> signal;
signal.connect(popup.show);

Popup movedPopup = std::move(popup);

signal.emit();

Here, calling emit() will trigger the instance of Slot belonging to movedPopup, which is as expected. However the value of this in the callback method render() will be that of popup.

This is because the callback function in Slot (which contains the this value of popup) was moved into the Slot instance in movedPopup - this is as intended for Slot copy/move semantics, but when that function contains a pointer to this we get unexpected results, like this.

@michaelcowan michaelcowan added the bug Something isn't working label Aug 25, 2019
@michaelcowan
Copy link
Owner Author

Just for context, pushing this onto the client could look something like this

class Popup {
public:
    Slot<> show = Slot<>(buildShowCallback());

    Popup() = default;

    Popup(Popup &&other) noexcept {
        this->show = std::move(other.show);
        this->show.setCallback(buildShowCallback());
    }
private:
    std::function<void()> buildShowCallback() {
        return [this]() { render(); };
    }
    void render() {
        Popup * pointer = this;
    }
};

Popup popup;
Signal<> signal;
signal.connect(popup.show);

Popup movedPopup = std::move(popup);

signal.emit();

😞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant