Iterator erase called before return?

I'm working with the EyeTab code available on Github (https://github.com/errollw/EyeTab/tree/master/EyeTab_SP2). I've been chasing down bad output values, and right before the function below returns the vector I need, there is a call to iterator erase which removes all the values and returns a bogus vector. I'd appreciate any help figuring out what is going on here!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  template<typename T>
std::vector<T> randomSubset(const std::vector<T>& src, typename std::vector<T>::size_type size) {

	if (size > src.size())
		throw std::range_error("Subset size out of range");

	std::vector<T> ret;
	std::set<int> vals;

	for (size_t j = src.size() - size; j < src.size(); ++j) {

		int rand = random(0, j); // generate a random integer in range [0, j]

		if (vals.find(rand) == vals.end())
			ret.push_back(src[rand]);
		else
			ret.push_back(src[j]);
	}
	//the value for ret is correct here, but it is not being returned, why?
	//why is iterator erase being called here and clearing out this value?
	return ret;
}
It looks ok. The problem must be in the calling function.
Line 14: You're doing a find on an empty set. I don't see anywhere that you add anything to vals. However, that does not explain an empty return vector.

Line 18: Have you verified that ret is intact when you exit the loop? How did you determine that erase is being called? Did you step into vector::erase when you exited the loop?

Line 20: There's no obvious call to erase. Have you verified with a debugger that ret is empty at line 21?
Line 14 - this was in the code when I inherited it, still a little confused about what it is doing...

When I step to Line 20, ret is an array of 5 points, which is what I'm looking for.

When I step into Line 21, I end up in iterator erase and the calling function gets a bogus vector.

Okay, now that I look at the calling function in more detail, I see:

randomSubset returned is the correct vector, but the vector sample is not getting set to the returned value.

1
2
std::vector<cv::Point2f> sample;
sample = randomSubset(edgePoints, 5);
Last edited on
Try with int first, which worked for me.
1
2
3
4
  std::vector<int> v;
  for (int i = 0; i < 200; ++i)
    v.push_back(i);
  std::vector<int> y = randomSubset(v,10);

then try with cv::Point2f
Int isn't working either in the program (I put this above the Point2f function call and stepped through it). The "randomSubset returned" is a vector of 10 ints, but y remains unchanged.

It is beginning to feel like I have a ghost in my machine! Thank you for your help!! If I can figure out what is going on, I'll update this thread.
Okay, I've stripped just these methods out into their own project and it works just fine.

I must be running into an issue with the rest of the code in the project I'm trying to use.

Watching every variable, even my const int is changing within the for loop...the for loop that doesn't even use it. Something is messed up...thank you for helping me narrow it down :)
Registered users can post here. Sign in or register to post.