从函数崩溃返回,仅在代码中的某个点之后
当我从函数返回时,我无法弄清楚为什么我的函数会导致程序崩溃.我做了一些调试,喜欢从函数返回导致它崩溃的那一刻.
I am having trouble figuring out why my function is crashing my program when I return from it. I did a bit of debugging and fond the moment where returning from the function causes it to crash.
void Find_contour()
{
//VAR
double threshold_val = 128;
int n_erode_dilate = 1;
Mat m = img_complete.clone();
cvtColor(m, m, CV_RGB2GRAY);
blur(m, m, Size(5, 5));
threshold(m, m, threshold_val, 255, CV_THRESH_BINARY);
erode(m, m, Mat(), Point(-1, -1), n_erode_dilate);
dilate(m, m, Mat(), Point(-1, -1), n_erode_dilate);
vector<vector<Point> > contours;
vector<Point> points;
//returning here is safe******
findContours(m, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
//returning after here crashes******
for (size_t i = 0; i<contours.size(); i++) {
for (size_t j = 0; j < contours[i].size(); j++) {
Point p = contours[i][j];
points.push_back(p);
}
}
vector<vector<Point>> contours_poly(contours.size());
vector<Rect> boundRect(contours.size());
vector<Point2f> center(contours.size());
for (int i = 0; i < contours.size(); i++)
{
approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
boundRect[i] = boundingRect(Mat(contours_poly[i]));
}
//drawContours(img_contour, contour, idx, colors[idx % 4]);
for (int i = 0; i< contours.size(); i++)
{
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
drawContours(drawing, contours, i, color, 2, 8, CV_RETR_LIST, 0, Point());
rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0);
}
}
这是函数,我已经标记了可以安全返回的位置和崩溃的位置,所以我假设它与我在那里的向量有关.
Here is the function, I have labelled where the it is safe to return and where it crashes, so I am assuming it has something to do with my vectors there.
我可能只是缺少一些基本的东西,这让我很难查找,提前谢谢!
I may just be missing something basic, which is making it hard for me to look up, thanks in advance!
嘿,我已经把范围缩小了 findContours
.单步执行代码,在安全的地方返回带我到 inline void Mat::release(
函数,而在 findContours
之后的任何地方都带我注释为 //TEMPLATE FUNCTION _Destroy_range
.然后转到 void deallocate
,此时会导致我的程序中断.并有一条消息.
Hey I have narrowed it down findContours
. stepping through the code, returning at a safe spot takes me to inline void Mat::release(
function, while anywhere after findContours
takes me something commented as // TEMPLATE FUNCTION _Destroy_range
. which then goes to void deallocate
which at this points causes a break in my program. and has a message.
图像检测 2.exe 中 0x51995042 (ucrtbase.dll) 处出现未处理异常:将无效参数传递给认为无效参数致命的函数.
我还要补充一点,这个错误只在函数返回或结束时出现,在findContour
期间没有错误.
I would also like to add that this error only occurs when the function returns or ends, and there are no errors during findContour
.
我对整个过程还很陌生,非常感谢大家的帮助.
I am fairly new to this whole process, and am very appreciative of everyone's help.
推荐答案
原来,OpenCV 3.0.0 和 Visual Studio 2015 存在一些兼容性问题.
Turns out, there is a bit of a compatibility issue with OpenCV 3.0.0 and Visual Studio 2015.
我已经用 Visual Studio 2013 进行了尝试,一切正常.
I have tried it with Visual Studio 2013 and everything works perfectly.
感谢那些提供帮助的人,感谢 @Simon Kraemer 提出的建议.
thank you to those who helped, and thank you to @Simon Kraemer for suggesting this.
相关文章