title: Opencv Findcontours cause heap error
body: <p>I am using opencv 2.49.</p>
  
  <p>But I am stuck by find contours function for hours.</p>
  
  <p>When I run the program in debug mode and the error box returns</p>
  
  <blockquote>
    <p>Debug assertion failed</p>
    
    <p>Program: ...
    File f:\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c</p>
    
    <p>Line: 1322</p>
    
    <p>Exception:_ CrtIsValidHeapPoionter(pUserData)</p>
  </blockquote>
  
  <p>And here is my function</p>
  
  <pre><code>HRESULT OpenCVHelper::DrawHand(Mat* pImg)        
  {
      vector&lt;vector&lt;Point&gt; &gt; contours;
      vector&lt;Vec4i&gt; hierarchy;
      cvtColor(*pImg, *pImg, CV_RGBA2GRAY);
      //Canny(*pImg, *pImg, 30,50);
      threshold( *pImg, *pImg, 50, 255,THRESH_BINARY);  
      if(pImg-&gt;type() == CV_8UC1)
      {
          findContours( *pImg, contours, hierarchy, CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
      }
          for( int i = 0; i&lt; contours.size(); i++ )
         {
          Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255),  rng.uniform(0,255) );
          Scalar color( rand()&amp;255, rand()&amp;255, rand()&amp;255 );
          drawContours( *pImg, contours, i, color, 2, 8, hierarchy, 0, Point() );
          }
  
  //contours.clear();
  //hierarchy.clear();
  cvtColor(*pImg, *pImg, CV_GRAY2RGBA);
  return S_OK;
  </code></pre>
  
  <p>}</p>
  
  <p>When I remove findcontour function there is no error.</p>
  
  <p>When I use findcontour it pops the error box showed above.</p>
  
  <p>When I add "contours.clear(); hierarchy.clear();" these two lines, there is no error message but the program still crashes.</p>
  
  <p>Anyone can help?</p>
  
  <p>EDIT 1. I find out the allocator for causing heap corruption, which is vector > contours; but I still don't know how to fix it. </p>