大家好:)
我在VC ++项目中使用了1.5.4-all(2014-10-22)(用于x86平台的Microsoft Visual C ++编译器18.00.21005.1)。
我的问题是一段时间后收到以下错误消息。错误发生的时间相差很远-有时在30秒后发生,有时在5分钟后发生。
我可以在第214行的LinearHashTable.h文件中找到错误的来源:
我有以下方法,其中将Shot(结构)添加到表中:
void ShotSimulationService::SimulateShot(Shot shot) {
MutexThreadLock.lock();
shots.insert(ShotsSetType::ValueType(SimulationShot(shot)));
errorCount = 0;
MutexThreadLock.unlock();
}
SimulateShot的调用来自另一个线程,而不是以下代码的处理:
void ShotSimulationService::Update(WebcamService* observable) {
if (shots.empty()) {
return;
}
try {
Mat frame = observable->GetLastImage().clone();
ShotsSetType::Iterator iter = shots.begin();
vector<Shot> deleteShots;
errorCount++;
while (iter != shots.end()){
if (iter->SimulateStartExplosion()) {
//simulate gun explosion
OverlayImage(frame, gunShotImg, iter->startPoint);
}
//simulate explosion
SimulationShot::SimulationHitStatus status = iter->status;
if (status == SimulationShot::SimulationHitStatus::UNKNOWN) {
if (detectionService.HasShotHitPlayer(frame, *iter)) {
iter->status = SimulationShot::HIT_PLAYER;
iter->SetCurrentPointAsEndoint();
//Notify that player was hit
playerHitQueue.enqueueNotification(new PlayerHitNotification(iter->hitPlayer));
}
}
if (iter->SimulateEndExplosion()) {
if (status == SimulationShot::HIT_PLAYER) {
int explosionx = iter->endPoint.x - robotExplosionHalfXSize > 0 ? iter->endPoint.x - robotExplosionHalfXSize : 0;
int explosionY = iter->endPoint.y - robotExplosionHalfYSize > 0 ? iter->endPoint.y - robotExplosionHalfYSize : 0;
OverlayImage(frame, robotExplosionImg, Point2i(explosionx, explosionY));
}
else {
// status == SimulationShot::HIT_WALL or UNKNOWN
int explosionx = iter->endPoint.x - wallExplosionHalfXSize > 0 ? iter->endPoint.x - wallExplosionHalfXSize : 0;
int explosionY = iter->endPoint.y - wallExplosionHalfYSize > 0 ? iter->endPoint.y - wallExplosionHalfYSize : 0;
OverlayImage(frame, robotExplosionImg, Point2i(explosionx, explosionY));
if (status != SimulationShot::HIT_WALL) {
iter->status = SimulationShot::HIT_WALL;
}
}
if (iter->IsSimulationFinished()) {
deleteShots.push_back(*iter);
}
}
else {
//simulate bullet
OverlayImage(frame, cheeseImg, iter->GetNextShotPoint());
}
++iter;
}
//delete finished simulations
MutexThreadLock.lock();
for each (Shot shot in deleteShots)
{
shots.erase(shot);
}
MutexThreadLock.unlock();
}
catch (cv::Exception& e) {
Logger& logger = Logger::get("Test");
logger.error(e.what());
}
}
Update方法通常称为安静方法-总是在有新的摄像头框架可用时使用。
错误的调用堆栈从以下行开始:
if (iter->SimulateEndExplosion()) {
在SimulateEndExplosion方法中,仅使用结构的成员:
bool SimulateEndExplosion() {
if (status == HIT_PLAYER) {
currPercentage = 1.0;
return true;
}
if (currPercentage < 1.0) {
return false;
}
++endExplosionCtr;
return endExplosionCtr <= maxEndExplosions;
}
有人知道为什么会出现此问题吗?
欢迎任何帮助和反馈!!我绝对不知道这里出了什么问题:(
谢谢!
最佳答案
在一个线程中迭代并插入另一个线程而又不保护两个线程中的互斥锁操作都会导致此问题。当您插入时,迭代器将无效,并且您将获得断言失败。您应该使用互斥量保护插入和迭代。
另外,使用互斥锁的方法也不安全,因为如果在lock()和unlock()之间抛出异常,则互斥锁不会被解锁。改用ScopedLock,在任何情况下RAII都会自动安全地完成此工作:
void ShotSimulationService::SimulateShot(Shot shot) {
Mutex::ScopedLock lock(MutexThreadLock);
shots.insert(ShotsSetType::ValueType(SimulationShot(shot)));
errorCount = 0;
// unlock will be called by ScopedLock destructor
}
关于c++ - LinearHashTable iter不可取消且iter不可,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27851837/