我的程序提示这样的错误vector iterators incompatible,代码停到以下for循环的位置,不知道为什么,这个代码是我一个线程在不停的在vector中选取符合条件的对象进行删除,这样的错误偶尔发生,请大家帮下忙,谢谢 C/C++ code?for (std::vector<CCommand*>::iterator it = this->CommandList.begin(); it!=this->CommandList.end();) { CCommand *cmdtmp = *it; if (cmdtmp->m_Send == true) { Lock(&cs_log); delete cmdtmp->command; delete cmdtmp; it = this->CommandList.erase(it); Unlock(&cs_log); }else{ it++; } } Lock(&cs_log); for (std::vector<CCommand*>::iterator it = this->CommandList.begin(); it!=this->CommandList.end();) { CCommand *cmdtmp = *it; if (cmdtmp->m_Send == true) { delete cmdtmp->command; delete cmdtmp; it = this->CommandList.erase(it); }else{ it++; } } Unlock(&cs_log); 需要包含整个 遍历删除操作 其它地方遍历的时候也要加这个锁
vector iterators incompatible 意思是跌代器不兼容
我当时定义了两个 vector 但是由于不小心指向错了, 就导致这样的问题产生, 虽然两个vector都有相同的类型.
|