Use copy of event subscribers list during event notification
Posted: 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
).
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