title: Unhandled exception - OpenCV - cvReleaseCapture and cvReleaseImage - C++
body: <p>I have a program that uses the OpenCV library (version 2.4.1) to capture video from my laptop's webcam (or any other connected camera) and save it to an .avi file. When I debug in Visual Studio 2010, I get an unhandled exception at the very end of the program, when either the CvCapture or the IplImage are being released. Here is the code:</p>
  
  <pre><code>    // WriteRealTimeCapturedVideo.cpp : Defines the entry point for the console application.
      #include "stdafx.h"
      #include "cv.h"
      #include "highgui.h"
      #include &lt;stdio.h&gt;
  
      int main()
      {
          CvCapture* capture = cvCaptureFromCAM( 1 ); //CV_CAP_ANY
          if ( !capture )
          {
              fprintf( stderr, "ERROR: capture is NULL \n" );
              getchar();
              return -1;
          }
          // Create a window in which the captured images will be presented
          cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
  
          double fps = cvGetCaptureProperty (capture, CV_CAP_PROP_FPS);
  
          CvSize size = cvSize((int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT));
  
          #ifndef NOWRITE
          CvVideoWriter* writer = cvCreateVideoWriter("Capture.avi", CV_FOURCC('M','J','P','G'), fps, size); //CV_FOURCC('M','J','P','G')
          #endif
  
          int width = (int)(cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH));
          int height = (int)(cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));
  
          IplImage* frame = cvCreateImage( cvSize( width,height ), IPL_DEPTH_8U, 1);
  
          while ( 1 )
          {
              // Get one frame
              frame = cvQueryFrame( capture );
              if ( !frame ) 
              {
                  fprintf( stderr, "ERROR: frame is null...\n" );
                  getchar();
                  break;
              }
              cvShowImage( "mywindow", frame );
              #ifndef NOWRITE
              cvWriteToAVI( writer, frame );
              #endif
              char c = cvWaitKey(33);
              if( c == 27 ) break;
          }
          #ifndef NOWRITE
          cvReleaseVideoWriter( &amp;writer );
          #endif
          cvDestroyWindow( "mywindow" );
          cvReleaseImage( &amp;frame );
          cvReleaseCapture( &amp;capture );
          return 0;
      }
  </code></pre>
  
  <p>I found that I have to have tbb.dll and tbb_debug.dll in the same directory as the source code (.cpp files) for the program to work. These dll's can be downloaded from Intel. </p>
  
  <p>The video capture works, that is, the window appears and displays the video, but the exception occurs no matter how I rearrange the release statements. If I remove the release statements (except for the VideoWriter), I don't get the exception, but then the .avi file produced cannot be opened. The program exits the while loop when the user presses the Esc key.</p>