Search code examples
c++windowsuser-interfacesignals-slots

How to get receiver's state in slot in AUI Framework?


I'm using AUI Framework which has similar signal-slot system to Qt. I'd like to access the state of receiver object. I have tried to capture the receiver object in C++ lambda:

auto model = _new<ATreeModel<AString>>(...);
_new<ATreeView>(model).connect(&ATreeView::itemSelected, model, [this, &model](ATreeModelIndex index){                                                
    auto selected = model->itemAt(index);
    // ...
});

But I've got [UI thread][SignalHandler][ERR]: Caught signal: Segmentation fault(11). Why?


Solution

  • That’s because you've captured model by reference. At the moment lambda is called (in your case, when treeview item is selected by user) model is out of scope so the reference to it is a dangling reference.

    model variable is a shared pointer so there's no need to store it in lambda by reference. Just store it by value: [this, model].

    As you've mentioned Qt, there's exact the same problem: https://forum.qt.io/topic/122849/connect-with-lambda-and-capture-by-reference-causes-segmentation-fault/2