title: Opencv Circle Detection - RAM loading
body: <p>I have recently been doing some circle detection in OpenCV and I have had some great success. The code below works wonderfully in C++ on a 32-bit platform. I do however have one small problem; as the program runs my RAM loads continues to load up. For example, when I start the program I am at 250mb, 30 seconds later I am at about 800mb. This continues and eventually overflows to the hard drive. I have tried to move the cvReleaseCapture inside the while loop but I just get the old "Exception at memory location....". Any ideas how I can clean this up? Thanks a lot. Here's the code:</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;cv.h&gt;
  #include &lt;highgui.h&gt;
  #include &lt;math.h&gt;
  
  
  int main( int argc, char **argv )
  {
      CvCapture *capture = 0;
      IplImage  *img = 0;
      int       key = 0;
  CvFont font;
  cvInitFont(&amp;font, CV_FONT_HERSHEY_PLAIN,1.0,1.0,0,1,CV_AA);
  
  capture = cvCaptureFromCAM( 0 );
  
  if ( !capture ) {
      fprintf( stderr, "Cannot open initialize webcam!\n" );
      return 1;
  } 
  
  cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
  
  while( key != 'q' ) {
      img = cvQueryFrame( capture ); 
      IplImage* gray = cvCreateImage( cvGetSize(img), 8, 1 );
      CvMemStorage* storage = cvCreateMemStorage(0);
      cvCvtColor( img, gray, CV_BGR2GRAY );
      cvSmooth( gray, gray, CV_GAUSSIAN, 9, 9 );
      CvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, gray-    &gt;height/4, 200, 100, 20, 100 );
      int i;
      for( i = 0; i &lt; circles-&gt;total; i++ )
      {
           float* p = (float*)cvGetSeqElem( circles, i );
           //cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), 2, CV_RGB(0,255,0), -1, 8, 0 );
           cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(0,255,0), 2, 8, 0 );
           cvLine (img, cvPoint(cvRound(p[0]+40),cvRound(p[1])), cvPoint(cvRound(p[0]),cvRound(p[1])), CV_RGB(0,255,0), 1, CV_AA,0);
           cvLine (img, cvPoint(cvRound(p[0]),cvRound(p[1]+40)), cvPoint(cvRound(p[0]),cvRound(p[1])), CV_RGB(0,255,0), 1, CV_AA,0);
           cvLine (img, cvPoint(cvRound(p[0]-40),cvRound(p[1])), cvPoint(cvRound(p[0]),cvRound(p[1])), CV_RGB(0,255,0), 1, CV_AA,0);
           cvLine (img, cvPoint(cvRound(p[0]),cvRound(p[1]-40)), cvPoint(cvRound(p[0]),cvRound(p[1])), CV_RGB(0,255,0), 1, CV_AA,0);
           cvPutText(img, "Meow",cvPoint(cvRound(p[0]+45),cvRound(p[1]+45)), &amp;font, CV_RGB(0,0,255)); 
  
      }
  
  
      if( !img ) break;       
      cvShowImage( "result", img ); 
      key = cvWaitKey( 1 );
      cvReleaseCapture( &amp;capture );
  }
  
  cvDestroyWindow( "result" );
  cvReleaseCapture( &amp;capture );    
  
  return 0;
  </code></pre>
  
  <p>}</p>