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.
This commit is contained in:
parent
3daac4883e
commit
d347223e42
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define event_manager_hh_INCLUDED
|
||||
|
||||
#include <poll.h>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "utils.hh"
|
||||
|
||||
|
@ -24,7 +25,7 @@ public:
|
|||
|
||||
private:
|
||||
std::vector<pollfd> m_events;
|
||||
std::vector<EventHandler> m_handlers;
|
||||
std::unordered_map<int, EventHandler> m_handlers;
|
||||
std::vector<int> m_forced;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user