Ask Your Question

Findcontours() deallocation error

asked 2017-05-08 03:23:47 -0500

renV gravatar image

updated 2017-05-09 05:40:08 -0500

I am using a function like this;

Mat large = imread(path+name);
Mat rgb;
if (large.rows > 2500 || large.cols > 1250)
    pyrDown(large, rgb);
    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


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

or "xmemory" class opening with these lines;

void deallocate(pointer _Ptr, size_type _Count)
    {   // deallocate object at _Ptr, ignore size
    _Mybase::deallocate(_Ptr, _Count);
edit retag flag offensive close merge delete


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)

berak gravatar imageberak ( 2017-05-08 03:48:48 -0500 )edit

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.

renV gravatar imagerenV ( 2017-05-08 05:17:11 -0500 )edit

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 gravatar imageberak ( 2017-05-08 08:40:58 -0500 )edit

@berak can you check my question again ? I updated the post

renV gravatar imagerenV ( 2017-05-09 05:47:44 -0500 )edit

your code runs fine on win & linux (it's not that)

berak gravatar imageberak ( 2017-05-09 05:54:05 -0500 )edit

can you try with delete vector pre-allocations and use these input images;

renV gravatar imagerenV ( 2017-05-09 06:03:27 -0500 )edit

305 contours found.

can you just try to setup a fresh (empty) project for this ? or compile on cmdline:

cl my.cpp opencv_core320.lib opencv_imgproc320.lib opencv_imgcodecs320.lib
berak gravatar imageberak ( 2017-05-09 06:17:39 -0500 )edit

@berak I guess we found the difference. I am using opencv version 2.4.12 but you are using 3.2

renV gravatar imagerenV ( 2017-05-09 06:25:22 -0500 )edit

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 ?

berak gravatar imageberak ( 2017-05-09 06:33:27 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-05-09 06:58:15 -0500

renV gravatar image

no I am testing with both, console application and CLR class library project. I am using Visual Studio 2015. Okey I am going to try in a new project.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2017-05-08 03:23:47 -0500

Seen: 851 times

Last updated: May 09 '17