Some interpret the message as in cppcheck is unsure if this is an error or not. I interpret this as cppcheck is sure that it is an error to access an iterator that is not guaranteed to be valid anymore.
Further I find the category [invalidCOntainer] a bit misleading as it should be the iterator that got invalidated not the container. The error text is also not helping.
About the false positive:
Iterators at or beyond the erased element are invalidated, not the ones before. Therefore, the code should be OK and the commented-out line would be incorrect.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Some interpret the message as in cppcheck is unsure if this is an error or not.
It says 'maybe' because it becomes invalid only if there is a resize. So calling reserve before hand would make things like push_back stable while the number of elements added are less than the reserved size.
Further I find the category [invalidCOntainer] a bit misleading as it should be the iterator that got invalidated not the container. The error text is also not helping.
That is a good point. It should probably be renamed to invalidContainerLifetime or invalidContainerReference. This actually checks both references and iterators.
Iterators at or beyond the erased element are invalidated, not the ones before. Therefore, the code should be OK and the commented-out line would be incorrect.
This is similiar to the FP of using reserve+push_back which we should probably do better at detecting in cppcheck.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
CppCheck 1.89 and 1.90 report
On this code
Some interpret the message as in cppcheck is unsure if this is an error or not. I interpret this as cppcheck is sure that it is an error to access an iterator that is not guaranteed to be valid anymore.
Further I find the category [invalidCOntainer] a bit misleading as it should be the iterator that got invalidated not the container. The error text is also not helping.
About the false positive:
Iterators at or beyond the erased element are invalidated, not the ones before. Therefore, the code should be OK and the commented-out line would be incorrect.
It says 'maybe' because it becomes invalid only if there is a resize. So calling
reserve
before hand would make things likepush_back
stable while the number of elements added are less than the reserved size.That is a good point. It should probably be renamed to
invalidContainerLifetime
orinvalidContainerReference
. This actually checks both references and iterators.This is similiar to the FP of using
reserve
+push_back
which we should probably do better at detecting in cppcheck.