Use copy of event subscribers list during event notification

If you found a bug in our library or on our website, please report it in this section. In this forum you can also make concrete suggestions or feature requests.

Moderators: CEGUI MVP, CEGUI Team

pianoman
Just popping in
Just popping in
Posts: 12
Joined: Fri Mar 20, 2009 23:22
Location: Nebraska, USA

Use copy of event subscribers list during event notification

Postby pianoman » Sun Nov 28, 2010 02:09

I wanted to make a suggestion as a result of a bug I spent a good deal of time tracking down: Have Event::operator() make a copy of d_slots and iterate over that to notify subscribers. That way subscribers are free to modify the list of subscribers (e.g. remove themselves) without causing a runtime exception to be thrown; instead, the event notification will go on its merry way notifying the subscribers that were subscribed at the time that the event occurred.

It is often very convenient for a subscriber to be able to remove itself or another subscriber. For example, I have a message box with a cancel button; when the cancel button gets pressed, the creator of the message box destroys the message box (resets its shared pointer to the message box), which in turn disconnects the cancel button subscriber. (I reuse the message box Window, instead of reloading it every time since it takes a noticeable amount of time to load). It would also be convenient because I and others would not run into this bug anymore (which might decrease forum traffic slightly too ;) ).

Jamarr
CEGUI MVP
CEGUI MVP
Posts: 812
Joined: Tue Jun 03, 2008 23:59
Location: USA

Re: Use copy of event subscribers list during event notification

Postby Jamarr » Mon Nov 29, 2010 22:41

I had a similar problem with another SDK I was using. I think that your approach would work, but would scale poorly. To avoid the unnecessary copy overhead, we could instead change the unsubscribe method to populate a queue (ptr to subscriber), and have operator() remove subscribers in this queue prior to iterating. This way only relevant subscribers are touched. I believe CEGUI already does something like this for window destruction.
If somebody helps you by replying to your thread, upvote him/her as a thanks! Make sure to include your CEGUI.log and everything you tried when posting! And remember that we are not magicians!


Return to “Bug Reports, Suggestions, Feature Requests”

Who is online

Users browsing this forum: No registered users and 5 guests