From 6bc3428307f26b6737555713d692fb4f839d732f Mon Sep 17 00:00:00 2001 From: Daniel Jour Date: Thu, 21 Nov 2019 11:35:23 +0100 Subject: [PATCH] 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. --- src/KeyFrame.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/KeyFrame.cpp b/src/KeyFrame.cpp index 2d4760bc..41c38ed8 100644 --- a/src/KeyFrame.cpp +++ b/src/KeyFrame.cpp @@ -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; } }