The iterator 'b' is invalidated by calling erase(b), which means that the subsequent call to ++b results in undefined behavior. This was identified by segfaults in the Matlab unit test on OS X.