From d347223e42a34945624b857f16820443d272ac26 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 6 Nov 2012 13:34:58 +0100 Subject: [PATCH] EventManager: store event handlers in an unordered_map instead of a vector If an event handler add or removes an event from the manager, it may then be moved in the vector, and if after that it access any of it's members (through this), it results in an invalid memory access. --- src/event_manager.cc | 6 +++--- src/event_manager.hh | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/event_manager.cc b/src/event_manager.cc index 12b23f7e..410b85b5 100644 --- a/src/event_manager.cc +++ b/src/event_manager.cc @@ -15,7 +15,7 @@ void EventManager::watch(int fd, EventHandler handler) throw runtime_error("fd already watched"); m_events.push_back(pollfd{ fd, POLLIN | POLLPRI, 0 }); - m_handlers.push_back(std::move(handler)); + m_handlers.emplace(fd, std::move(handler)); } void EventManager::unwatch(int fd) @@ -25,7 +25,7 @@ void EventManager::unwatch(int fd) if (m_events[i].fd == fd) { m_events.erase(m_events.begin() + i); - m_handlers.erase(m_handlers.begin() + i); + m_handlers.erase(fd); return; } } @@ -39,7 +39,7 @@ void EventManager::handle_next_events() { if ((res > 0 and m_events[i].revents) or contains(m_forced, m_events[i].fd)) - m_handlers[i](m_events[i].fd); + m_handlers[m_events[i].fd](m_events[i].fd); } m_forced.clear(); } diff --git a/src/event_manager.hh b/src/event_manager.hh index 1e8d5a87..9f21fa06 100644 --- a/src/event_manager.hh +++ b/src/event_manager.hh @@ -2,6 +2,7 @@ #define event_manager_hh_INCLUDED #include +#include #include "utils.hh" @@ -24,7 +25,7 @@ public: private: std::vector m_events; - std::vector m_handlers; + std::unordered_map m_handlers; std::vector m_forced; };