1

I have a C++ project using Qt 5.1.1 and OpenCV 2.4.6. The image processing algorithm runs in a separate thread. All works fine but if I call OpenCV function findContours() the program crashes with a stack overflow message (right in the first time this function is called, not like it was already called several times before) "Unhandled exception at 0x56ec9a47 in SARA.exe: 0xC00000FD: Stack overflow."

I found someone with the same problem but in his case it was only a matter of changing the project to Visual Studio 2010...but in my case, my project is already in VS2010.

The algoritm runs fine if I create a separate console project, that just calls the image processing algorithm, but the same code inside a thread in my Qt project show a stack overflow! If I remove the findContours() function, all woks as it should. In both projects I use the same libs and debug dlls (versioned as xxx246d.dll), and I am compiling the program as debug.

I tried to make the stack larger by changing the Properties -> Configuration Properties -> Linker -> System -> Stack Reserve Size option, but then the program still crashes, with a different message, saying "Unhandled exception at 0x76e5c41f in SARA.exe: Microsoft C++ exception: Concurrency::scheduler_resource_allocation_error at memory location 0x14c7adc8.."

I don't think it's a code problem, since it runs fine as a console aplication, but if anyone wants to see it, it goes as:

QImage SaraVisualControl::findCircles(void)

{
Mat imgInput = imread("M:/Desktop/PsEyeRight1.jpg", CV_LOAD_IMAGE_COLOR); 

Mat roiInput(imgInput, Rect(Point(205, 72), Point(419,285)));
Mat imgContours = roiInput.clone();

cvtColor(imgContours, imgContours, CV_BGR2GRAY); 
GaussianBlur(imgContours, imgContours, Size(3, 3), 0, 0, 4);            
threshold(imgContours, imgContours, 150, 255, THRESH_BINARY); // Ou ler o Otsu uma vez e usar ele

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;    

findContours(imgContours, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_NONE, Point(0, 0)); // Program crashes here!

vector<RotatedRect> ellipses;
RotatedRect ellipse;

for(int i = 0; i < contours.size(); i++)
{
    if(contours[i].size() >= 5)
    {
        ellipse = fitEllipse(contours[i]);   
        ellipses.push_back(ellipse);
    }
    else
    {
        Point2f center;
        float radius = 0.0;
        minEnclosingCircle(contours[i], center, radius);
        ellipses.push_back(RotatedRect(center, Size2f(radius, radius), 0.0));
    }
}

cvtColor(imgContours, roiInput, CV_GRAY2BGR); 

int baseLine = 0;
const double fontScale = 0.5;
const int thickness = 1;   

for(int i = 0; i < ellipses.size(); i++)
{        
    cv::ellipse(roiInput, ellipses[i], CV_RGB(255, 0, 0), 1, 8);
    Size textSize = getTextSize(std::to_string((long long)i + 1), FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, thickness, &baseLine); 
    putText(roiInput, std::to_string((long long)i + 1), Point(ellipses[i].center.x - (textSize.width/2), ellipses[i].center.y + (textSize.height/2)), FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, 
        Scalar(255, 255, 255), thickness, 8, false);         
}      
return QImage((uchar*)roiInput.data, roiInput.cols, roiInput.rows, QImage::Format_RGB32);
}
Community
  • 1
  • 1
Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173
  • see this: http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/find_contours/find_contours.html try to use Canny edge detector before calling findContours() – azer89 Feb 07 '14 at 05:44
  • Why's that? This same code runs fine in a console application... – Michel Feinstein Feb 07 '14 at 06:16
  • Ah sorry, didn't see that. I've encountered this problem long time ago. The problem was solved after I changed all projects in my solution to VC 2010. I also used Qt Qt version VC 2010 too – azer89 Feb 07 '14 at 06:59
  • Yes...but I am already using them :/ – Michel Feinstein Feb 07 '14 at 08:17
  • Hmmm, okay, what do you mean by using different thread? – azer89 Feb 07 '14 at 12:43
  • The GUI is in one thread and the image processing in another...another thread gets the images to process and sends to the GUI and to the image processing thread, so the user can see whats on the camera, and the image processing happens on the background – Michel Feinstein Feb 07 '14 at 12:57
  • Did you find a solution to this yet, MFeinstein? I found out that many of the OpenCV routines infact use parallel_for internally, this causes some kind of thread-pool to be allocated for doing these operations in parallel. Now, if you have setup such a huge stack reservation space, keep in mind its for EACH thread (and your application may have much more threads than you created, because some libraries, like OpenCV, internally create even more). Given that 32 bit processes are limited to < 2 GB of memory, I found that while allocation threads, I found that CreateThread HRESULT 0x80070008..... – MShekow May 08 '14 at 12:22
  • ... fails, the HRESULT stands for being out of storage (memory). I similarly had to increase stack reservation space to a more than 10 MB, but with 100 MB (as you suggested) I would get exactly these crashes you described. I started increasing the stack reservation space limit in steps of 10 MB and ended up with 50 MB working fine now. Still, I've gotta say that the original stack overflow message that you get with default 1 MB stack reservation space is already bullshit, I do know for a fact that (in my application) the stack is _NOT_ exceeded, so I recon it's a Visual C++ bug. – MShekow May 08 '14 at 12:25
  • @NameZero912 I did not solve this yet, and if you want a more detailed explanation of what happened, look at this post:http://stackoverflow.com/questions/21998478/c-compilation-introduce-errors Honestly I dont have the time for now to continue to explore this error, but I really would like to solve it – Michel Feinstein May 09 '14 at 14:52

1 Answers1

1

I faced a similar situation today with OpenCV 2.4.8 while building a Debug project on Qt Creator 3.1.2 using using MSVC 2013 as compiler.

Suddenly, I noticed that the .pro file was linking my application to OpenCV release libraries:

LIBS += -L"C:\\opencv\\build\\x86\\vc12\\lib" \
    -lopencv_core248 \
    -lopencv_highgui248 \
    -lopencv_imgproc248

when it should in fact, be linking to OpenCV debug libraries:

LIBS += -L"C:\\opencv\\build\\x86\\vc12\\lib" \
    -lopencv_core248d \
    -lopencv_highgui248d \
    -lopencv_imgproc248d

Remember kids: the project build type should match OpenCV libraries type.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • Well this isn't my case unfortunately...Everything works fine in Debug, I have the crash only in Release, all my dlls match...I opened a new thread and more complete one in here: http://stackoverflow.com/questions/21998478/c-compilation-introduce-errors Still can't find the solution..... – Michel Feinstein Sep 15 '14 at 21:15