【发布时间】:2011-06-09 18:20:29
【问题描述】:
我有一个 C++ 应用程序,它发生了一次无法重现的断言失败。这是一次失败的代码:
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); // How could this fail?
return theLastCount;
}
不知何故,循环完成后,theLastCount 不为零。
根据我对逻辑的阅读,这应该是不可能的,除非:
- 其他一些线程影响了 actionQueue(我认为这是不可能的)。
- 发生了一些短暂的内存损坏。
我在这里错过了什么愚蠢的东西,我的代码中是否有错误?请注意,在我看到这一点时,theLastCount 应该被初始化为 1,因为该向量有两个元素。
【问题讨论】:
-
它NOT怎么会失败呢?请参阅 DeadMG 的回答。
-
@PigBen 如果内部返回被击中。
-
但是这个值没有经过测试。不参加考试就不会失败。