Findcontours() deallocation error
I am using a function like this;
Mat large = imread(path+name);
Mat rgb;
if (large.rows > 2500 || large.cols > 1250)
{
pyrDown(large, rgb);
}
else
{
rgb = large.clone();
}
cv::Mat smallx;
cvtColor(rgb, smallx, CV_BGR2GRAY);
Mat grad,connected,bw;
Mat morphKernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
cv::morphologyEx(smallx, grad, MORPH_GRADIENT, morphKernel);
cv::threshold(grad, bw, 100, 255, THRESH_BINARY + THRESH_OTSU);
morphKernel = getStructuringElement(MORPH_RECT, Size(9, 1));
cv::morphologyEx(bw, connected, MORPH_CLOSE, morphKernel);
Mat mask = Mat::zeros(bw.size(), CV_8UC1);
vector<vector<Point>> contours(50000);
vector<Vec4i> hierarchy(50000);
cv::findContours(connected, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
Some input images prompt exception when function returns, it throws me to "_Mybase::deallocate(_Ptr, _Count);"
code block in xmemory0 class. I must catch this kind of errors and prevent this process from unexpected suspensions
--------------UPDATE--------------
After my debug, I found that exception thrown by
cv::findContours(connected, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
statement. --------------UPDATE2-------------- I tried that code block with 30 different input but it gave error in a few image. after findcontours it thrown an exception break and show me this message.
Unhandled exception at 0x74477fb2 (ucrtbase.dll) in ImageRecognizer.exe: 0xC0000409: 0xc0000409.
in that time , "vector" class opening in my Visual Studio with these lines ;
~vector() _NOEXCEPT
{ // destroy the object
_Tidy();
}
or "xmemory" class opening with these lines;
void deallocate(pointer _Ptr, size_type _Count)
{ // deallocate object at _Ptr, ignore size
_Mybase::deallocate(_Ptr, _Count);
}
please check you linker settings, make sure you strictly link opencv release libs against release build and debug libs against debug build.
(btw, please do not pre-allocate those vectors, they'll get overwritten anyway)
I checked my linker settings and it has already fine, I know that pre-allocate of those vectors is not professional practise but before I make this, I get more exceptions. Most probably my exception related with vector size or elements. but I couldn't detect.
you won't be able to catch such a thing, it's thrown from the c-runtime, not from opencv.
the heap got corrupted, that's unrecoverable.
it's not your code, but the way you build your program. and again, problems like yours hint at a problem with the linker. please check again, also check, if you're using dynamically linked CRT (you have to.)
@berak can you check my question again ? I updated the post
your code runs fine on win & linux (it's not that)
can you try with delete vector pre-allocations and use these input images;
http://imgur.com/a/yAClR
http://imgur.com/a/yAClR
305 contours found.
can you just try to setup a fresh (empty) project for this ? or compile on cmdline:
@berak I guess we found the difference. I am using opencv version 2.4.12 but you are using 3.2
you're wrong, it runs fine on 2.4, too
again, it's something wrong in your VS project. make a new one, try without IDE, whatever.
are you using weird shit like MFC or CLI ?