Skip to content

Commit

Permalink
Keyframe::AddPoint() fix: reallocation invalidates iterator
Browse files Browse the repository at this point in the history
Points.push_back can (and will) cause reallocation, which invalidates
the candidate iterator.  Thus better use an (integer) index.
  • Loading branch information
musteresel committed Nov 21, 2019
1 parent 504ea0c commit 6bc3428
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/KeyFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ void Keyframe::AddPoint(Point p) {
// New point needs to be inserted before candidate; thus move
// candidate and all following one to the right and insert new
// point then where candidate was.
Points.push_back(p); // Make space; could also be a dummy point.
std::move_backward(candidate, end(Points) - 1, end(Points));
*candidate = p;
size_t const candidate_index = candidate - begin(Points);
Points.push_back(p); // Make space; could also be a dummy point. INVALIDATES candidate!
std::move_backward(begin(Points) + candidate_index, end(Points) - 1, end(Points));
Points[candidate_index] = p;
}
}

Expand Down

0 comments on commit 6bc3428

Please sign in to comment.