title: cvFindContours always returns 0 - OpenCV
body: <p>I'm calling the <code>cvFindContours</code> function inside a separate thread that I've created to handle all OpenCV work while another is kept for OpenGL stuff.</p>
  <p>I noticed that my <code>cvFindContours</code> function always returns 0 when this code is executed inside a separate thread. It worked fine before, when executed in the main thread itself. I used breakpoints and Watches to evaluate value changes. everything else (variables) gets values except for <code>contourCount</code> (value: 0).</p>
  <p>Any clue?</p>
  <pre><code>// header includes goes here
  CvCapture* capture = NULL;
  IplImage* frame =  NULL;
  IplImage* image;
  IplImage* gray;
  IplImage* grayContour;
  CvMemStorage *storage;
  CvSeq *firstcontour=NULL; 
  CvSeq *polycontour=NULL;
  int contourCount = 0;   
      
  DWORD WINAPI startOCV(LPVOID vpParam){
    
      capture = cvCaptureFromCAM(0); // NOTE 1
      capture = cvCaptureFromCAM(0);  
      frame = cvQueryFrame(capture);  
      image = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U,3);
      gray = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U,1);
      grayContour = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U,1);
      storage = cvCreateMemStorage (0);
      firstcontour=NULL; 
      
      while(1){
              frame = cvQueryFrame(capture);
              cvCopy(frame,image);
              cvCvtColor(image,gray,CV_BGR2GRAY);     
              cvSmooth(gray,gray,CV_GAUSSIAN,3);
              cvThreshold (gray, gray, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
              cvNot(gray,gray);
              cvCopy(gray,grayContour);
              
              contourCount=cvFindContours (grayContour, storage, &amp;firstcontour, sizeof (CvContour), 
      CV_RETR_CCOMP); 
                              polycontour=cvApproxPoly(firstcontour,sizeof(CvContour),storagepoly,CV_POLY_APPROX_DP,3,1); // Error starts here (Pls refer to stack trace)   
      }
           // goes on...
  }
  
  int main(int argc, char** argv){
  
       DWORD qThreadID;
       HANDLE ocvThread = CreateThread(0,0,startOCV, NULL,0, &amp;qThreadID);
       initGL(argc, argv); //some GL intitialization functions
       glutMainLoop(); // draw some 3D objects
       CloseHandle(ocvThread);
       return 0;
  }
  </code></pre>
  <p>NOTE1: these lines had to be duplicated due to the error mentioned at <a href="https://stackoverflow.com/questions/8081062/how-to-avoid-video-source-capture-source-selection-in-opencv-2-3-0-visual">How to avoid &quot;Video Source -&gt; Capture source&quot; selection in OpenCV 2.3.0 - Visual C++ 2008</a></p>
  <p>Environment:
  OpenCV 2.3.0
  Visual C++ 2008</p>
  <p><strong>EDIT</strong></p>
  <p><strong>Traces</strong></p>
  <blockquote>
  <p>opencv_core230d.dll!cv::error(const cv::Exception &amp; exc={...})  Line 431 C++</p>
  <p>opencv_imgproc230d.dll!<strong>cvPointSeqFromMat</strong>(int seq_kind=20480, const void * arr=0x00000000, CvContour * contour_header=0x01a6f514, CvSeqBlock * block=0x01a6f4f4)  Line 47 + 0xbd bytes C++</p>
  <p>opencv_imgproc230d.dll!<strong>cvApproxPoly</strong>(const void * array=0x00000000, int header_size=88, CvMemStorage * storage=0x017e7b40, int method=0, double parameter=3.0000000000000000, int parameter2=1)  Line 703 + 0x28 bytes C++</p>
  <p>Project.exe!startOCV(void * vpParam=0x00000000)  Line 267 + 0x24 bytes    C++</p>
  </blockquote>
  <p>All this stuff boils down to the function <code>CV_Assert( arr != 0 &amp;&amp; contour_header != 0 &amp;&amp; block != 0 )</code> in <code>cvPointSeqFromMat</code> and it fails since <code>arr</code> it requires is empty.</p>