Skip to content

Commit 6bc3428

Browse files
committed
Keyframe::AddPoint() fix: reallocation invalidates iterator
Points.push_back can (and will) cause reallocation, which invalidates the candidate iterator. Thus better use an (integer) index.
1 parent 504ea0c commit 6bc3428

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/KeyFrame.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ void Keyframe::AddPoint(Point p) {
7575
// New point needs to be inserted before candidate; thus move
7676
// candidate and all following one to the right and insert new
7777
// point then where candidate was.
78-
Points.push_back(p); // Make space; could also be a dummy point.
79-
std::move_backward(candidate, end(Points) - 1, end(Points));
80-
*candidate = p;
78+
size_t const candidate_index = candidate - begin(Points);
79+
Points.push_back(p); // Make space; could also be a dummy point. INVALIDATES candidate!
80+
std::move_backward(begin(Points) + candidate_index, end(Points) - 1, end(Points));
81+
Points[candidate_index] = p;
8182
}
8283
}
8384

0 commit comments

Comments
 (0)