Fix undefined behavior on destroying an event context

The destructor of the context class accidentally incremented the iterator
twice per iteration. If the number of event handlers was odd, the
destructor ended up incrementing the end iterator, which is UB.

I rewrote the whole destructor. It's unnecessary to manually remove event
handlers from the list because the list will do it automatically when it's
destroyed.
This commit is contained in:
Jyrki Vesterinen 2016-07-22 21:05:08 +03:00
parent 681322bf29
commit 8a296386e3

View file

@ -132,15 +132,15 @@ void context::set_focus(const sdl_handler* ptr)
context::~context()
{
if (!handlers.empty()) {
for (handler_list::iterator it = handlers.begin(); it != handlers.end(); ++it) {
if ((*it)->has_joined()) {
(*it)->has_joined_ = false;
}
if ((*it)->has_joined_global()) {
(*it)->has_joined_global_ = false;
}
it = handlers.erase(it);
for (sdl_handler* h : handlers)
{
if (h->has_joined())
{
h->has_joined_ = false;
}
if (h->has_joined_global())
{
h->has_joined_global_ = false;
}
}
}