LinearHashTable iter不是dereferencable而不是

时间:2015-01-09 00:11:06

标签: c++ visual-c++ poco-libraries

Hy all:)

我在我的VC ++项目中使用1.5.4-all(2014-10-22)(x86平台的Microsoft Visual C ++编译器18.00.21005.1)。

我的问题是我在一段时间后收到以下错误消息。发生错误的时间差异很大 - 有时它发生在30秒之后,有时发生在5分钟之后。 enter image description here

我可以在第214行找到LinearHashTable.h文件中的错误源: enter image description here

我有以下方法将Shot(struct)添加到表中:

    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方法经常被称为安静 - 总是在新的网络摄像头帧可用时。

错误的callstack从以下行开始:

    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;
        }

有人知道为什么会出现这个问题吗?

欢迎任何帮助和反馈!我绝对不知道这里出了什么问题:(

谢谢!

1 个答案:

答案 0 :(得分:1)

在一个线程中迭代并插入另一个线程而不用两个线程中的互斥锁保护操作将导致此问题;当你插入时,迭代器将失效,你将得到断言失败。您应该使用互斥锁保护插入和迭代。

此外,您使用互斥锁的方式并不安全,因为如果在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
    }