Avoid potential segfault due to invalid vector.end() after erase()

This commit is contained in:
Vegard Kippe 2024-11-15 13:52:14 +01:00
parent 3188ac713a
commit 7ece35defe

View File

@ -504,11 +504,14 @@ recalculateGradientAndUpdateData_(GradPairItr& grad_itr,
updateGradVector_(name, other_grads, grad->grad); updateGradVector_(name, other_grads, grad->grad);
} }
else { else {
for (auto it = other_grads.begin(); it != other_grads.end(); it++) { auto new_end = std::remove_if(other_grads.begin(), other_grads.end(), [&name](const GradPair& pair)
if (it->first == name) { {
other_grads.erase(it); return (name==pair.first);
deleteGrad_(name, !increase); }
} );
if (new_end != other_grads.end()) {
this->deleteGrad_(name, !increase);
other_grads.erase(new_end, other_grads.end());
} }
} }
} }