Ask Your Question
0

segmentation fault extracting contour element in replacement statement

asked 2014-05-19 12:25:55 -0600

Ralph058 gravatar image

Hi I get a segmentation fault when I extract an element from a contour in a replace statement but not in cout.

The first line prints fine, but there is a segmentation fault with the second line.

std::cout << "inContour y: " << inContour[0].y << '\n';

Yold = inContour[0].y;

At the point in the above code, I have isolated the largest contour and sorted it by the y terms. All appears OK with the sorted contour.

Yold has been declared with int Yold;

and inContour has been declared as vector<point> inContour; several lines above this. Moving the declaration of Yold to this line did not help.

Point is supposed default as a 2 parameter integer. Specifically declaring it as Point2i did not help.

Any suggestions?

edit retag flag offensive close merge delete

Comments

i can't see your replacement statement. this might need a bit mode code.

berak gravatar imageberak ( 2014-05-19 13:15:21 -0600 )edit

Declaration of vector of points vector<Point2i> inContour; vector<Point2i> sortedContour; vector<Point2i> prunedContour; NOTE: this was also done using Point, with the same results

The following is the code at issue: std::stable_sort (inContour.begin(), inContour.end(), compare_y); FileStorage fs("SortedContour.yml", FileStorage::WRITE); fs << "inContour" << inContour; fs.release(); std::cout << "inContour y: " << inContour[0].y << '\n';

Yold = inContour[0].y;

sortedContour[0].x = inContour[0].x;
sortedContour[0].y = inContour[0].y;

While it handles the cout OK, it generates a segment fault with the line Yold = inContour[0].y;

If I comment it out, then it generates one with the next line.

stable_sort works OK.

Ralph058 gravatar imageRalph058 ( 2014-05-22 11:12:06 -0600 )edit

stable_sort was verified by manually reading the YAML file.

I haven't figured out how to past code cleanly.

Ralph058 gravatar imageRalph058 ( 2014-05-22 11:14:57 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-05-22 14:09:58 -0600

Ralph058 gravatar image

updated 2014-05-22 14:13:07 -0600

While I have no idea what the problem was with

Yold = inContour[0].y;

by compiling it with the debug flag, it went away and it stayed away when I set the release flag.

Having said that, the line

sortedContour[0].x = inContour[0].x;

caused a segment fault because sortedContour had not been sized. I will allow that the Yout line was always OK and I was misinterpreting the condition. That would make more sense than a self healing program.

Adding the lines:

int sz2=inContour.size();

sortedContour.resize(sz2);

corrected the latter segment fault and the code executed properly.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-05-19 12:25:55 -0600

Seen: 480 times

Last updated: May 22 '14