I have a C ++ application that had a one-time failure that I cannot reproduce. Here is the incorrect code:
unsigned int test(std::vector<CAction> actionQueue) {
unsigned int theLastCount = actionQueue.size() - 1;
std::vector<CAction>::const_reverse_iterator rItr = actionQueue.rbegin();
std::vector<CAction>::const_reverse_iterator rEndItr = actionQueue.rend();
for (; rItr != rEndItr; ++rItr, --theLastCount) {
const CAction &fileAction = *rItr;
if (fileAction.test()) {
continue;
}
return theLastCount;
}
assert(theLastCount == 0);
return theLastCount;
}
Somehow, after the loop ended, theLastCount was not equal to zero.
From my reading of logic, this should be impossible if:
- Some other side of the thread produced an actionQueue (which I think is impossible).
- Some damage occurred with short-term memory.
I'm missing something stupid here, is there an error in my code? Please note that in the case when I saw this, theLastCount had to be initialized with one, since the vector had two elements.
source
share