0

So I am using a list of vectors and my code seems to break it so that the for loop does not exit.

Here is the code which works:

for(possible_planes_it = possible_planes.begin(); possible_planes_it != possible_planes_end; possible_planes_it++)
{
    if(static_cast<float>(good_matches.size()) >= static_cast<float>((matches.size())*0.8))
    {
        possible_planes_it->push_back(Plane(area, *center_it, keypoint, descriptor, count));
    }
    else
    {
        list.push_back(vector<Plane>());
        list.back().push_back(Plane(area, *center_it, keypoint, descriptor, count));
    }
}

This causes the iterators to break:

for(possible_planes_it = possible_planes.begin(); possible_planes_it != possible_planes_end; possible_planes_it++)
{
        if(static_cast<float>(good_matches.size()) >= static_cast<float>((matches.size())*0.8))
        {
        if(possible_planes_it->back().getTimestamp() < count) // Means that there has not been a match found this round
        {
            possible_planes_it->push_back(Plane(area, *center_it, keypoint, descriptor, count));
            possible_planes_it->back().setNumberOfGoodMatches(good_matches.size());
        }
        else
        {
            if(possible_planes_it->back().getNumberOfGoodMatches() < good_matches.size())
            {
                possible_planes_it->pop_back(); // If a better match has been found, remove the last vector entry and push the new one
                possible_planes_it->push_back(Plane(area, *center_it, keypoint, descriptor, count));
                possible_planes_it->back().setNumberOfGoodMatches(good_matches.size());
            }
            else
            {
                list.push_back(vector<Plane>());
                list.back().push_back(Plane(area, *center_it, keypoint, descriptor, count));
            }
        }
    }
    else
    {
        list.push_back(vector<Plane>());
        list.back().push_back(Plane(area, *center_it, keypoint, descriptor, count));
    }
}

Does anyone have an idea what I do so horribly wrong :D?

user2175762
  • 271
  • 1
  • 6
  • 13
  • Haha. Good point. Tagged as for-loop, yet not a single `for` anywhere in the code... – Mad Physicist Jan 17 '14 at 15:04
  • `Does anyone have an idea what I do so horribly wrong :D?` you forgot the loop. – user1810087 Jan 17 '14 at 15:06
  • 1
    Your `static_cast` stinks to high heaven. Shutting up the compiler when you're doing something wrong is likely to get you stuck scratching your head in front of a non-cooperative computer for a few hours. Do yourself a favour and remove the cast, then try to understand and correct the problem. – kuroi neko Jan 17 '14 at 15:08
  • you say you have an infinite loop; but also that the second code snippet "causes the iterators to break". What exactly do you mean? – Pandrei Jan 17 '14 at 15:17
  • Now with for loops :D. My bad.... – user2175762 Jan 17 '14 at 16:27
  • @kuroineko: Well, I am working with opencv. And with that statement I am trying to figure out if 80% of the found matches in the picture are good matches. Whats so fundamentally wrong about it? – user2175762 Jan 17 '14 at 16:32
  • @Pandrei: With that I mean that the second for loop is not exited. I am stuck in there. While the first snippet exits just fine. – user2175762 Jan 17 '14 at 16:33

1 Answers1

1

Check out vector's pop_back and push_back pages, specifically, see iterator validity and notice how your iterators will be invalidated if a relocation occurs.

See also What is iterator invalidation?

Community
  • 1
  • 1
FRob
  • 3,883
  • 2
  • 27
  • 40
  • OK, thanks for showing me that. So are you saying that if one of the vectors in my list is relocated all the points become invalid? Even the list iterator?! – user2175762 Jan 17 '14 at 16:41
  • Also note that in the first snippet I am not having this error, even though I am pushing data into my vectors. That's the weird part... – user2175762 Jan 17 '14 at 16:47