c++ - Vector gets iterated more times than size() -


i've got piece of code:

for (std::vector<marker>::iterator = markers.begin(); != markers.end(); ++it) {     if (it->getdots().size() < 3) {         markers.erase(it);     } } 

in 1 of test inputs (the app image analysis) segfault. tried debug code (to no avail) , noticed 1 thing. when asking gdb p markers.size() receive $9 = 3. expect loop iterate 3 times, surprisingly (at least) 5 times. in fifth iteration there's segfault. noticed it's not dereference of *it (here it->) causes error. it's it->getdots(), simple getter.

i write in c++ rarely, might simple mistake, neither debugging, nor googling brought solution. help?

i'd emphasize, on various different inputs (slightly different images) function works properly, it's harder me trace bug down.

vector::erase invalidates iterators pointing element being erased, , elements follow. it becomes invalid, , ++it expression on next loop iteration exhibits undefined behavior.

the best way code logic erase-remove idiom.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -