title: OpenCV insufficient memory
body: <p>I am running the following code on Visual C++ 2008 and OpenCV 2.1. It works for a while (say 3 minutes) and then aborts with an error saying</p>
  
  <p>"Insufficient Memory (Failed to allocate 92610 bytes) in unknown function, file ........\ocv\opencv\src\cxcore\cxalloc.cpp, line 52"</p>
  
  <p>There must be some memory leak somewhere (probably with image creation) but I can't seem to get hold of it.</p>
  
  <pre><code>#include "cv.h"
  #include "highgui.h"
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  #include &lt;math.h&gt;
  
  using namespace std;
  
  void main()
  {
      int i, j;
      int **arr = new int*[480], blob[6][8]={0};
      int max, maxi=-10, maxj=-10, div=80;
      int xmax=480, ymax=640;
      int frameH, frameW, fps, numFrames;
      double hue, sat, lum;
      int maxcolor, mincolor, maxcolval, mincolval;
      char key='a';
      CvScalar pix, destpix, destpix2, destpix3;
      IplImage *img, *dest, *hsv;
  
      for(i=0; i&lt;480; i++)
          arr[i] = new int[640];
  
      CvCapture *capture = cvCaptureFromCAM(0);
      if(!capture)
      {
          cout&lt;&lt;"Cannot read video!!!!";
          exit(0);
      }
  
      frameH    = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
      frameW    = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
      fps       = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
      numFrames = (int) cvGetCaptureProperty(capture,  CV_CAP_PROP_FRAME_COUNT);
      printf("Width=%d Height=%d FPS=%d Count=%d\n", frameH, frameW, fps, numFrames);
  
      cvNamedWindow("win1", CV_WINDOW_AUTOSIZE);
      cvMoveWindow("win1", 10, 10);
      cvNamedWindow("win2", CV_WINDOW_AUTOSIZE);
      cvMoveWindow("win2", 600, 300);
  
      destpix.val[0]=0;
      destpix.val[1]=255;
      destpix.val[2]=0;
      destpix2.val[0]=0;
      destpix2.val[1]=0;
      destpix2.val[2]=0;
      destpix3.val[0]=255;
      destpix3.val[1]=255;
      destpix3.val[2]=255;
  
      while(key != 'q')
      {
          max=0;
          maxi=-10;
          maxj=-10;
  
          img = cvQueryFrame(capture);
          if(img == 0)break;
  
          dest = cvCloneImage(img);
          hsv = cvCloneImage(img);
  
          cvCvtColor(img, hsv, CV_BGR2HSV);
  
          for(i=0; i&lt;xmax; i++)
              for(j=0; j&lt;ymax; j++)
              {
                  arr[i][j]=0;
                  blob[i/div][j/div]=0;
              }           
  
          cout&lt;&lt;endl&lt;&lt;cvGet2D(hsv, 5, 5).val[0];
          //Looping through each pixel
          for(i=0; i&lt;xmax; i++)
          {
              for(j=0; j&lt;ymax; j++)
              {
                  //Getting the current pixel (i, j)
                  pix = cvGet2D(hsv, i, j);
  
                  //Setting all pixels to black
                  cvSet2D(dest, i, j, destpix2);
  
                  hue = pix.val[0];
                  sat = pix.val[1];
                  lum = pix.val[2];
  
                  //Looking for color red
                  if((hue&lt;5 || hue&gt;177) &amp;&amp; sat&gt;120 &amp;&amp; lum&gt;60)
                  {
                      arr[i][j] = 1;
                      cvSet2D(dest, i, j, destpix);
                  }
  
                  /*//Looking for color green
                  if((hue&gt;90 &amp;&amp; hue&lt;100) &amp;&amp; sat&gt;120 &amp;&amp; lum&gt;60)
                  {
                      arr[i][j] = 1;
                      cvSet2D(dest, i, j, destpix);
                  }*/
  
                  /*//Looking for color blue
                  if((hue&gt;100 &amp;&amp; hue&lt;110) &amp;&amp; sat&gt;120 &amp;&amp; lum&gt;60)
                  {
                      arr[i][j] = 1;
                      cvSet2D(dest, i, j, destpix);
                  }*/
  
                  /*//Looking for color yellow
                  if((hue&gt;30 &amp;&amp; hue&lt;40) &amp;&amp; sat&gt;120 &amp;&amp; lum&gt;60)
                  {
                      arr[i][j] = 1;
                      cvSet2D(dest, i, j, destpix);
                  }*/
              }   
          }
  
          //Counting the blobs in each grid
          for(i=0; i&lt;xmax; i++)
          {
              for(j=0; j&lt;ymax; j++)
              {
                  if(arr[i][j])
                  {
                      blob[i/div][j/div]++;
                  }
              }
          }
  
          //Finding the grid with the largest blob
          for(i=0; i&lt;xmax/div; i++)
              for(j=0; j&lt;ymax/div; j++)
                  if(blob[i][j]&gt;max)
                  {
                      max=blob[i][j];
                      maxi=i;
                      maxj=j;
                  }
  
          if(max&gt;200)
          {
              //Borders
              for(i=maxi*div; i&lt;maxi*div+2; i++)
                  for(j=maxj*div; j&lt;maxj*div+div; j++)
                      cvSet2D(dest, i, j, destpix3);
  
              for(i=maxi*div+div-2; i&lt;maxi*div+div; i++)
                  for(j=maxj*div; j&lt;maxj*div+div; j++)
                      cvSet2D(dest, i, j, destpix3);
  
              for(i=maxi*div; i&lt;maxi*div+div; i++)
                  for(j=maxj*div; j&lt;maxj*div+2; j++)
                      cvSet2D(dest, i, j, destpix3);
  
              for(i=maxi*div; i&lt;maxi*div+div; i++)
                  for(j=maxj*div+div-2; j&lt;maxj*div+div; j++)
                      cvSet2D(dest, i, j, destpix3);
  
              //Center
              for(i=maxi*div+(div/2)-5; i&lt;maxi*div+(div/2)+5; i++)
                  for(j=maxj*div+(div/2)-5; j&lt;maxj*div+(div/2)+5; j++)
                      cvSet2D(dest, i, j, destpix3);
          }
  
          //Creating Windows
          //cvCvtColor(fin, dest, CV_HSV2BGR);
          key = cvWaitKey(20);
          cvShowImage("win1", dest);
          cvShowImage("win2", img);
      }
  
      cvWaitKey(0);
      cvReleaseCapture(&amp;capture);
      cvReleaseImage(&amp;dest);
      cvDestroyWindow("win1");
      cvDestroyWindow("win2");
  }
  </code></pre>