title: OPENCV Weird Error
body: <p>I am using Background Subtraction and want to display the contents. Somehow the code seems to break all the time due to a memory exception. The error seems to be in cvCopy function. Can't figure it out.</p>
  
  <pre><code>#include "cv.h"
  #include "highgui.h"
  #include "opencv2\core\operations.hpp"
  #include "opencv2\core\core.hpp"
  #include "opencv2\core\types_c.h"
  #include "opencv\cxcore.h"
  #include &lt;opencv2/opencv.hpp&gt;
  
  using namespace cv;
  using namespace std;
  
  int main(int, char**)
  {
      bool flag=0;
      VideoCapture cap(0); // open the default camera
      VideoCapture cap1(0);
      if(!cap.isOpened())  // check if we succeeded
          return -1;
  
      Mat gray,bg,result,frame,result1,final,frame1;
      //CvMemStorage*     contours = NULL;
  
      cap&gt;&gt;frame;
      cvtColor(frame,bg,CV_BGR2GRAY);
  
      namedWindow("GRAY",1);
  
      for(;;)
      {
          //final = Mat::zeros(mGreenScale.rows, mGreenScale.cols, CV_8UC3);
          cap &gt;&gt; frame; // get a new frame from camera
          cap1 &gt;&gt; frame1;
          cvtColor(frame, gray, CV_BGR2GRAY);
          absdiff(gray,bg,result);
          threshold(result,result1,50,255,THRESH_BINARY);
          //cvCopy(const CvArr* src, CvArr* dst, const CvArr* mask=NULL)¶
          //cvCopy(&amp;frame1, &amp;final, &amp;result1);
          //findContours(result1,contours, ;CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
          //drawContours(final,contours,-1,CV_RGB(0,255,0));
          //imshow("GRAY",result1);
          //imshow("GRAY", result);
          imshow("GRAY1",final);
  
          if(flag)
          {
              imshow("BG",bg);
          }
          //if(waitKey(0)==27) break;
          if(waitKey(1)==32) 
          {
              cvtColor(frame,bg,CV_BGR2GRAY);
              flag=!flag;
          }
          if(waitKey(1)==27) 
          {
              break;
          }
      }
      // the camera will be deinitialized automatically in VideoCapture destructor
      return 0;
  }
  </code></pre>