Ask Your Question
0

Editing core.hpp crashes during run-time

asked 2012-08-26 20:20:55 -0600

roboto1986 gravatar image

So I asked a question on StackOverflow and was wondering if anyone came across this. My program works fine until I add an un-initialized member variable to Point_ class defined in core.hpp. When compiled, this variable wont be initialized and will contain a random variable and will be 4 bytes larger than the original. Why is assigning a vector of that type to another vector of the same type make the iterator crash with: "Debug Assertion Failed!" "Expression: vector iterator not dereferencable"

What made the iterator 'not dereferencable' when adding this un-initialized variable?

Thanks!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2012-08-27 01:43:51 -0600

sammy gravatar image

updated 2012-08-27 01:44:39 -0600

Many internal algorithms exploit the fact that the Point_ class only has the x and y coordinate data.

An example (that seems to be the source of your error) is transparent conversion between vector<Point_<> > and cv::Mat.

Internally, it initializes a matrix using the vector data, assuming the matrix row size to be 2 (2 dimensions in vector) and col size to be vector size. Then, it accesses the raw data using a pointer:

mat.data = &(vec[0].x) // this is the address of the first element in the vector

If the data struct only has two member vars, the next matrix column can be easily accessed by

((int*)mat.data)[j + width * i] // where w == 2.

But in the modified struct, the bool adds 32 more bits, and the conversion fails.

Conclusion: do not mess with the OpenCV core unless you know what you're doing.

Anyway, that is a bad programming practice, and there are many clean ways to solve your problem without modifying the core. By example, create your own data struct:

struct MyPoint
{
    cv::Point p;
    bool myData;
}

Final word: before rushing to build a vector of points (original points) and a separate vector of bools, you should be aware of this "special property" of std::vector<bool>: http://stackoverflow.com/questions/6781985/is-the-use-of-stdvectorbool-objects-in-c-acceptable-or-should-i-use-an-al

edit flag offensive delete link more

Comments

1

Yep, in the book "Effective STL" whole item was dedicated for this issue: "Item 18: Avoid using vector<bool>"

Michael Burdinov gravatar imageMichael Burdinov ( 2012-08-27 03:09:09 -0600 )edit

Thank you both for taking the time to look over this. So to summarize, and please correct me if I'm wrong, OpenCV copies only the x and y data to mat.data from _Point so later on because I added my variable, the bytes don't align any more and I basically over-run my iterator's pointer? So this was more of an experiment than a barrier to what I'm going to do but it's always nice to know why things break the way they do :)

roboto1986 gravatar imageroboto1986 ( 2012-08-27 19:35:14 -0600 )edit

Yep, it thinks the second x position is actually after the first y, but ity will find there the bool value, and everything else will not be aligned anymore.

sammy gravatar imagesammy ( 2012-08-28 00:56:42 -0600 )edit

Question Tools

Stats

Asked: 2012-08-26 20:20:55 -0600

Seen: 393 times

Last updated: Aug 27 '12