findContours throws exception intermittently - 2.4.5
We are using version 2.4.5 (x64 Release/Windows/VS2012/VC11) and call findContours() for each image we process, but seemingly randomly, it will thrown an unhandled exception. We saw this problem a couple of months ago, and we could not reproduce it and at the same time haven't seen it since then. It just started happening randomly when none of the code around this bit of processing has changed at all.
In our software, we've registered a callback to write out a memory dump in the event that an unhandled exception happens, and here is the stack trace:
STACK_TEXT:
00000000`50289d90 000007fe`fadade25 : 000007fe`eabe9ec0 00000000`5028b1b8 00000000`00000100 00000000`50289e80 : KERNELBASE!RaiseException+0x39
00000000`50289e60 00000000`77c9d561 : 00000000`00000000 00000000`5028c030 00000000`00000000 00000000`00000000 : msvcr110!_CxxCallCatchBlock+0x161
00000000`50289f30 000007fe`eaa9f3ae : 00000000`77d603d0 00000000`ec06faa0 00000000`5028c920 00000000`00000013 : ntdll!RcFrameConsolidation+0x3
00000000`5028c730 000007fe`eaa9adad : 00000000`5028ccb8 00000000`5028ccb8 00000000`00000001 00000000`5028cb50 : opencv_imgproc245!cvFindContours+0x3de [d:\downloads\opencv_2_4_5\opencv\modules\imgproc\src\contours.cpp @ 1688]
00000000`5028c8e0 000007fe`f1e1f89e : 00000000`0032aae2 00000000`00000000 00000000`00000000 00000000`00000000 : opencv_imgproc245!cv::findContours+0x18d [d:\downloads\opencv_2_4_5\opencv\modules\imgproc\src\contours.cpp @ 1713]
Function where this is called from--edgeImage is always computed the same way for each image buffer we process and is of grayscale type. We zero it out first and then call Canny() with it passed in as a second parameter. I can share more of that code if necessary, but we do not see "edge image type to be passed..." in our logging.
void FindFeaturePoints(cv::Mat& edgeImage) const
{
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
if (edgeImage.type() != CV_8U)
_logger->Fatal("Edge image type to be passed to findContours is NOT grayscale!");
findContours(edgeImage, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_NONE, Point(0, 0) );
}
The line 1688 of contours.cpp is:
do
{
count++;
contour = cvFindNextContour( scanner );
}
while( contour != 0 ); //1688
(which WinDbg can be one line off on, and this means it's actually something in cvFindNextContour())
I'm just not sure why this would happen randomly--it seems to occur once every several million or so images we process. Again, we've been using the same bit of code around this and haven't seen the problem in months.
Any help is greatly appreciated, especially with taking the stack trace addresses and applying them to what might be going on in the actual findContours() function.
you shall stop using cvFindNextContour, it is old deprecated c-API, see this for changing the code in c++ manner
I'm not using cvFindNextContour--it is called by cvFindContours internally, which is called by findContours, the function we're using. If you noticed the stack trace, line 1688 of contours.cpp is what's throwing the exception.
You are right... strange behaviour... What version of OpenCV?
2.4.5. I'm not ruling out the possibility that there is some sort of heap corruption going on elsewhere in our code base, and the findContours() function call/variables are the targets of it. It's throwing the exception but the source of the problem lies in another area. Really strange for this to happen out of nowhere but always be in the same place though...
You can update to OpenCV 2.4.11, maybe it has been fixed meanwhile... Or even OpenCV 3.0.0 (but this I find it a lot of changes)