I am writing a small UI for my program. I have the method onMouseMotion()
, which I can call in one of two ways (see code); if I call it through std::function
, then !=
operator in the for loop stop condition produces the run-time exception vector iterators incompatible
. Why?
class Widget : public EventHandler
{
protected:
/* ... */
std::vector<Widget *> children_;
std::function<bool(Event &)> func_;
private:
bool onMouseMotion(Event &event);
/* ... */
};
Widget::Widget()
{
/* ... */
func_ = std::bind(&Widget::onMouseMotion, this, std::placeholders::_1);
/* ... */
}
bool Widget::processEvent(Event &event)
{
if (event.getType() == ui::EventType::MouseMotionEvent) {
/* Method 1 - onMouseMotion works ok */
onMouseMotion(event);
/* Method 2 - onMouseMotion throws */
//func_(event);
return true;
}
}
bool Widget::onMouseMotion(Event &event)
{
/* exception occurs on the next line, only when using Method 2 above */
for (auto child = children_.rbegin(); child != children_.rend(); ++child) {}
}
Updates:
- program is single-threaded.
- the exception is thrown when entering the
for
loop, zero iterations occur. - compiling with MSVC.
- same exception with an empty
for
loop. - rewritten examples to illustrate the
std::function
problem.