Page 1 of 1

How can I remove an event if it is executing? [solved]

Posted: Sat Mar 21, 2009 19:11
by Toge
Hi.

My code is:

Code: Select all

...

button->subscribeEvent(PushButton::EventMouseClick, Event::Subscriber (&MyClass::onEventClick, this));

...


bool MyClass::onEventClick (const CEGUI::EventArgs& pEventArgs)
{
       button->removeAllEvents();
       // I want to subscribe another event to this button in this place
}



When I click the button, I get a segmentation fault.


Thanks.





Edit: I solved it. I override the old event by the new event

Code: Select all

...

button->subscribeEvent(PushButton::EventMouseClick, Event::Subscriber (&MyClass::onEventClick, this));

...


bool MyClass::onEventClick (const CEGUI::EventArgs& pEventArgs)
{
       button->subscribeEvent(PushButton::EventMouseClick, Event::Subscriber (&MyClass::newHandler, this));
}





Edit 2:

Finally, I needed to use a "deadlist". When a Button was clicked, its event was pushed back in the list. The list was updated (cleared) every frame.

Posted: Sat Mar 21, 2009 23:14
by CrazyEddie
Hi,

The 'removeAllEvents' function actually removes the event objects themselves as opposed to subscribed handlers :D

I'm also not sure if the 'fix' is what you want - since it leaves the original handler function connected also - so every time it's clicked, you get another copy of the second handler added; it will subsequently be invoked multiple times ;)

The current 'correct' method to disconnect an event subscriber is to save the connection object returned when you subscribe, and later call the 'disconnect' member function on that connection to disconnect.

HTH

CE.

Posted: Sun Mar 22, 2009 09:04
by Toge
You are right. When I click button, multiple events are executed. Im go to use "disconnect". Thanks.

Posted: Sun Mar 22, 2009 10:07
by Toge
I have this code now:

Code: Select all


Event::Connection connection;

...

connection = button->subscribeEvent(PushButton::EventMouseClick, Event::Subscriber (&MyClass::onEventClick, this));

...


bool MyClass::onEventClick (const CEGUI::EventArgs& pEventArgs)
{
       connection->disconnect();
}



However, the program ends with segmentation fault. Is it possible that a Event disconnects itself?

Posted: Sun Mar 22, 2009 11:42
by CrazyEddie
Hi,

I should have spotted the deliberate mistake last night, though it was late and I was knackered :lol:

Currently there are issues when disconnecting event subscribers from within a subscriber attached to the same event priority queue. The main reason this is the case is because it messes up the STL iterator used to iterate over subscribers.

What you might have to do is maintain a 'dead list' of connections to be removed at the end of the frame (or something).

Sorry about the confusion :?

CE