Fix crash by reintroduce splice.

This fixes a crash that would be caused by memory incorrectly being freed
when removed from a list. Relying on splice stops that from happening.

This also fixes an exit condition when deleting handlers as depth could
sometimes not be decremented.

Finally, checks are made to made sure that an sdl_handler is either
exclusively in the global context or a different context, ensuring
that they cannot be in both.
This commit is contained in:
Andreas Löf 2016-07-29 00:20:05 +12:00
parent 1cfad2404f
commit abc64e62c8

View File

@ -67,6 +67,7 @@ bool context::remove_handler(sdl_handler* ptr)
const handler_list::iterator i = std::find(handlers.begin(), handlers.end(), ptr);
if(i == handlers.end()) {
--depth;
return false;
}
@ -210,6 +211,10 @@ void sdl_handler::join() {
void sdl_handler::join(context &c)
{
if (has_joined_global_) {
leave_global();
}
if(has_joined_) {
leave(); // should not be in multiple event contexts
}
@ -264,6 +269,10 @@ void sdl_handler::leave()
void sdl_handler::join_global()
{
if(has_joined_) {
leave();
}
if(has_joined_global_) {
leave_global(); // should not be in multiple event contexts
}
@ -335,8 +344,7 @@ bool has_focus(const sdl_handler* hand, const SDL_Event* event)
focus_handler(thief_hand);
// Position the previously focused handler to allow stealing back
handlers.push_back(*foc);
handlers.erase(foc);
handlers.splice(handlers.end(), handlers, foc);
return thief_hand == hand;
}