In EventObject::postEvent, there is a small piece of code as following
// For every direct observer // Directly call its function for (auto i = directObservers.begin(); i != directObservers.end(); i++) { if (i->first == e.m_type) { std::vector<Observer>& observers = i->second; ** for (std::vector<Observer>::iterator j = observers.begin(); j != observers.end(); j++) { // If function of observer does not exist, remove observer if (j->second != nullptr) { // Call the function j->second(ePtr.get()); } else { j = i->second.erase(j); } }** } }
As marked by bold fonts, if the iterator j has null function, it will be erased. The potential problem is that: “j++” in the for statement, which is run regardless of whether function of j is null or not. When j’s function happens to be null, the ‘erase’ will move j to the next iterator just after the erased one, whose function will not be called because j will further be moved to the next position by “j++” statement. So if there is a ‘observer’ whose function is null, its next ‘observer’ will be ignored in any way. Is it intended for any reason? Or it is a bug? EventObject is the basic class of iMSTK. So I think I should report it here.
If it is a bug, the following code is recommended:
for (std::vector::iterator j = observers.begin(); j != observers.end() { // If function of observer does not exist, remove observer if (j->second != nullptr) { // Call the function j->second(ePtr.get()); j++; } else { j = i->second.erase(j); } }
Yours, Ainray wwzhang0421@163.com