lundi 25 juillet 2016

Segmentation fault while iterating through and erasing some elements from std::map?

I am using several std::map type data containers which are the following:

    std::map<int, std::vector< cv::Point > > mapGoodContours;
    std::map<int, EllipseProperties> ellipsePropertiesMap;
    std::map<int, float> m_markerRadiusMap;
    std::map<int,cv::Point2f> markers;//This is part of another class

I iterate through these containers and erase some elements after those elements meet certain condition as shown in the following code.

    auto radiusMapCounter = m_markerRadiusMap.begin();
    auto markerCounter = frames.markers.begin();
    auto goodContoursCounter = mapGoodContours.begin();

    if(m_markerRadiusMap.size()==ellipsePropertiesMap.size() && frames.markers.size()==ellipsePropertiesMap.size()
            && mapGoodContours.size()==ellipsePropertiesMap.size())
    {
        for(auto ellipsePropertyCounter = ellipsePropertiesMap.begin(); ellipsePropertyCounter != ellipsePropertiesMap.end(); ellipsePropertyCounter++)
        {

            float upperLimit = (float)m_upperFactor*(float)ellipsePropertyCounter->second.radius;
            float lowerLimit = (float)m_lowerFactor*(float)ellipsePropertyCounter->second.radius;
            if(ellipsePropertyCounter->second.minDistanceFromOtherEllipse>upperLimit
                        || ellipsePropertyCounter->second.minDistanceFromOtherEllipse<lowerLimit)
            {
                 ellipsePropertiesMap.erase(ellipsePropertyCounter);
                 m_markerRadiusMap.erase(radiusMapCounter);
                 frames.markers.erase(markerCounter);
                 mapGoodContours.erase(goodContoursCounter);
            }
            else
            {
                 smallContours.push_back(goodContoursCounter->second);
            }

            radiusMapCounter++;
            markerCounter++;
            goodContoursCounter++;
        }
    }

I am baffled to find that sometimes I have segmentation faults as shown in the image. enter image description here The fault specifically points to the line with the code radiusMapCounter++;

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire