FindContours() causing heap corruption on VS2013? [closed]

asked 2016-03-26 11:54:03 -0500

Gazoo gravatar image

Hey fellow forum-goers,

I ran into some trouble with FindContours() in OpenCV 3.0 and OpenCV 3.1 using Visual Studio 2013. At first I assumed I am at fault (and I'm not entirely unsure that I've not messed up somewhere still), but I'm slowly beginning to wonder if perhaps FindContours() actually does cause heap corruption?

The following straight forward code seems to cause heap corruption for me:

std::vector<std::vector<cv::Point> > contours;
cv::Mat inp( cv::Size( 400, 400 ), CV_8UC1 );
cv::findContours( inp, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE );

I use Cinder at a framework for lots of my coding, but given its robust history, I'd be surprised if anything in an empty skeleton setup would interfere with the OpenCV code I've posted above. This code run on its own causes this error in Vs2013:

Debug Assertion Failed!

Program: D:\Code\FindContoursTest\vc2013\x64\Debug\FindContoursTest.exe
File: f:\dd\vctools\crt\crtw32\misc\dbgheap.c
Line: 1424

Expression: _pFirstBlock == pHead

I've found numerous posts regarding the use (or rather misuse) of OpenCV, and perhaps something is bad with the code above, but as far as I've been able to ascertain, it certainly shouldn't cause any heap corruption. I came across this post by lowlander256 who claims that it is indeed an issue in OpenCV.

Perhaps someone can help me at least figure out if the isolated code above should run fine?



edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-10-10 14:54:37.659886


your code is correct, this sounds more like a linker issue.

please double check if you're accidentally use opencv debug libs with release builds, or vice versa, if you're using the correct vs libs matching your compiler, also x86/x64

berak gravatar imageberak ( 2016-03-26 11:58:28 -0500 )edit

Hey berak, thanks for the comment. I've just looked things over, and unless linking with libs found in the vc12 folder (which as far as I know is actually Visual Studio 2013) is wrong, then I think all the linking is correct. Given that the pre-compiled binaries for OpenCV 3.1 are x64 only, I've paid particular attention to proper linking. Additionally, I can without any issue use OpenCV's other functionality, like cv::solvePnP, so I'm fairly convinced it's not a linker issue.

Gazoo gravatar imageGazoo ( 2016-03-26 12:05:52 -0500 )edit

While I'm no expert, I feel fairly confident that there is something amiss with OpenCV running on Visual Studio 2013 and using FindContours (C++).

I've made use of the C implementation as a substitute which works without a hitch.

So... Bugger, I guess? I should look into adding it as a bug.

Gazoo gravatar imageGazoo ( 2016-03-28 14:43:48 -0500 )edit

Run into the same problem and wasted a lot of time trying to figure it out. There was a suggestion somewhere to use vector<mat> instead of vector<vector<point> > for contours, but this does not solve the problem. Could be some obscure issue with MS STL runtime, no idea. Had to go back to OpenCV 2.4.9.

Alex N gravatar imageAlex N ( 2016-06-15 07:16:52 -0500 )edit

I am also stuck with this.. Planning to try with C implementation. If I can mix the best from both the world.

subhadipna gravatar imagesubhadipna ( 2018-01-27 10:09:55 -0500 )edit

@subhadipna please do not use the deprecated c-api for anything new. it is dead since 2010, not "the best of 2 worlds"

berak gravatar imageberak ( 2018-01-27 10:16:17 -0500 )edit