//triggerEvent is called in engine update every frame.
void EventManager::triggerEvent(sf::Event* event) {
if (!listeners_.count(event->type)) return;
std::map<std::string, listenerFunction>::iterator iterator = listeners_[event->type].begin();
while (iterator != listeners_[event->type].end()) {
int listeners_size = listeners_[event->type].size();
iterator->second(event); //execute listener (inside my custom listener func is unregisterListener called). The unregList will put listener_update in update_listeners_ vector.
//BTW, the function requires event pointer.
updateEvents(); //if update_listeners_ is not empty, updateEvents initializes all listeners from update_listeners_ to listeners_[event->type]. listeners_[event->type] is map<string(name), function>. And listeners is map<sf::Event::EventType, std::map<std::string, listenerFunction>>
//So the error is caused by iterator->second because the function calls unregisterListener which deletes itself while iterating is happening.
++iterator;
}
}
I'm trying to iterate through the whole map to execute listeners (functions). But if an listener calls unregisterListener(name)(deletes an item from map) and deletes itself, the Microsoft Visual C++ Runtime Library will open with error message: map/set iterator not incrementable.
I understand the error, but I don't have idea how to fix it. This is the hardest bug i ever had. I will appreciate any help, and if something is unclear I will answer. Thanks :).
std::map<T1, T2> map;
//populate map
for (auto i = map.begin(), e = map.end(); i != e;){
auto current = i++;
//now you can do whatever you want with current
}
void EventManager::triggerEvent(sf::Event* event) {
if (!listeners_.count(event->type)) return;
//std::map<std::string, listenerFunction>::iterator iterator = listeners_[event->type].begin();
std::map<std::string, listenerFunction> map = listeners_[event->type];
for (auto i = map.begin(), e = map.end(); i != e;) {
auto current = i++;
i->second(event);
updateEvents();
}
//while (iterator != listeners_[event->type].end()) {
// int listeners_size = listeners_[event->type].size();
// iterator->second(event); //execute listener (inside my custom listener func is unregisterListener called). The unregList will put listener_update in update_listeners_ vector.
// //BTW, the function requires event pointer.
// updateEvents(); //if update_listeners_ is not empty, updateEvents initializes all listeners from update_listeners_ to listeners_. listeners_ is map<string(name), function>
// //So the error is caused by iterator->second because the function calls unregisterListener which deletes itself while iterating is happening.
// ++iterator;
//}
}