Getting Event Sender from Dereferenced Pointer Object


#1

Hello, I’m struggling to get a pointer to an object via the ofAddListener callback. I used the method mentioned here; How to get the *sender from a dispatched event?

But the object is null

MyGroup * myGroup;
MyElement * myElement;

void setup() {
    myElement = &myGroup->addElement();
    ofAddListener(myElement->valueUpdated, this, &MyClass::valueUpdatedEvent);
}

void valueUpdatedEvent(const void * sender, int & i) {
    MyElement * el = static_cast <MyElement *> (const_cast <void *> (sender));
    ofLog() << "Received Element" << el->getName(); 
    // EXC_BAD_ACCESS
}

#2

How are you notifying the event?


#3

Hey,

void setValue(int val, bool notify) {
    config["data"]["value"] = val;
    if (notify) ofNotifyEvent(valueUpdated, val);
}

#4

you need to notify it sending the sender as well otherwise it’ll be null:

ofNotifyEvent(valueUpdated, val, this);

i’ve used this supposing this is the object that you consider the sender but otherwise change it with whatever you want to be received as the sender on the listener side


#5

Yep that’s it, thank you!