title: C++ First-chance exception
body: <p>I have wrote a shape detection code with c++.
  I am using Visual Studio 2013 Express Desktop Edition.</p>
  
  <p>When I run the program it will give following error.</p>
  
  <p>First-chance exception at 0x54EE3C77 (opencv_imgproc244d.dll) in Final.exe: 0xC0000005: Access violation reading location 0x05958000.</p>
  
  <p>If there is a handler for this exception, the program may be safely continued.</p>
  
  <p>Following is the code sample.</p>
  
  <pre><code>#include "stdafx.h"
  #include "opencv2/highgui/highgui.hpp"
  #include "opencv2/imgproc/imgproc.hpp"
  #include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;thread&gt;  
  #include &lt;cv.h&gt;
  #include &lt;highgui.h&gt;
  #include &lt;windows.h&gt;
  
  
  
  
  void shapeDetectionNew(IplImage* img)
  {
  
  try
  {
  
      //converting the original image into grayscale
      IplImage* imgGrayScale = cvCreateImage(cvGetSize(img), 8, 1);
      cvCvtColor(img, imgGrayScale, CV_BGR2GRAY);
  
      //thresholding the grayscale image to get better results
      cvThreshold(imgGrayScale, imgGrayScale, 128, 255, CV_THRESH_BINARY);
  
      CvSeq* contour;  //hold the pointer to a contour
      CvSeq* result;   //hold sequence of points of a contour
      CvMemStorage *storage = cvCreateMemStorage(0); //storage area for all contours
  
      //finding all contours in the image
      cvFindContours(imgGrayScale, storage, &amp;contour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
  
      //***********************************
      cv::Mat img = cv::imread("F:\\My Works\\Opencv\\Shape_Detection_Images\\shape.jpg");
  
      //convert IplImage to Mat
      //cv::Mat img(iplImg);
  
      cv::Mat gray;
      cv::cvtColor(img, gray, CV_BGR2GRAY);
  
      // Use Canny instead of threshold to catch squares with gradient shading
      cv::Mat bw;
      cv::Canny(gray, bw, 0, 50, 5);
  
      // Find contours
      std::vector&lt;std::vector&lt;cv::Point&gt; &gt; contours;
      cv::findContours(bw.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
  
      std::vector&lt;cv::Point&gt; approx;
      cv::Mat dst = img.clone();
  
      //***********************************
  
      for (int i = 0; i &lt; contours.size(); i++)
      {
          // Approximate contour with accuracy proportional
          // to the contour perimeter
          cv::approxPolyDP(cv::Mat(contours[i]), approx, cv::arcLength(cv::Mat(contours[i]), true)*0.02, true);
  
          // Skip small or non-convex objects 
          if (std::fabs(cv::contourArea(contours[i])) &lt; 100 || !cv::isContourConvex(approx))
              continue;
  
          if (approx.size() == 3)
          {
              setLabel(dst, "TRI", contours[i]);    // Triangles
          }
          else if (approx.size() &gt;= 4 &amp;&amp; approx.size() &lt;= 6)
          {
              // Number of vertices of polygonal curve
              int vtc = approx.size();
  
              // Get the cosines of all corners
              std::vector&lt;double&gt; cos;
              for (int j = 2; j &lt; vtc + 1; j++)
                  cos.push_back(angle(approx[j%vtc], approx[j - 2], approx[j - 1]));
  
              // Sort ascending the cosine values
              std::sort(cos.begin(), cos.end());
  
              // Get the lowest and the highest cosine
              double mincos = cos.front();
              double maxcos = cos.back();
  
              // Use the degrees obtained above and the number of vertices
              // to determine the shape of the contour
              if (vtc == 4 &amp;&amp; mincos &gt;= -0.1 &amp;&amp; maxcos &lt;= 0.3)
                  setLabel(dst, "RECT", contours[i]);
              else if (vtc == 5 &amp;&amp; mincos &gt;= -0.34 &amp;&amp; maxcos &lt;= -0.27)
                  setLabel(dst, "PENTA", contours[i]);
              else if (vtc == 6 &amp;&amp; mincos &gt;= -0.55 &amp;&amp; maxcos &lt;= -0.45)
                  setLabel(dst, "HEXA", contours[i]);
          }
          else
          {
              // Detect and label circles
              double area = cv::contourArea(contours[i]);
              cv::Rect r = cv::boundingRect(contours[i]);
              int radius = r.width / 2;
  
              if (std::abs(1 - ((double)r.width / r.height)) &lt;= 0.2 &amp;&amp;
                  std::abs(1 - (area / (CV_PI * std::pow((double)radius, 2)))) &lt;= 0.2)
                  setLabel(dst, "CIR", contours[i]);
          }
      }
  
      cv::imshow("Shape_Detection", dst);
  
  
  }
  catch (int e)
  {
      throw e;
  }
  }
  
  int main()
  {
      try
      {
  
      // Create CvCapture object to grab data from the webcam
      CvCapture* pCapture;
  
      // Start capturing data from the webcam
      pCapture = cvCaptureFromCAM(1);
  
      // Define the IplImage pointers we're going to use as globals
      IplImage* pFrame;
  
      IplImage* pProcessedFrame;
      IplImage* tempFrame;
      pFrame = cvQueryFrame(pCapture);
  
      //Create the low threshold slider
      // Format: Slider name, window name, reference to variable for slider, max value of slider, callback function
      cvCreateTrackbar("Low Threshold", "Edge_Detection", &amp;lowSliderPosition, maxLowThreshold, onLowThresholdSlide);
  
      // Create the high threshold slider
      cvCreateTrackbar("High Threshold", "Edge_Detection", &amp;highSliderPosition, maxHighThreshold, onHighThresholdSlide);
  
      // Create a greyscale image which is the size of our captured image
      pProcessedFrame = cvCreateImage(cvSize(pFrame-&gt;width, pFrame-&gt;height), IPL_DEPTH_8U, 1);
  
      // Create a frame to use as our temporary copy of the current frame but in grayscale mode
      tempFrame = cvCreateImage(cvSize(pFrame-&gt;width, pFrame-&gt;height), IPL_DEPTH_8U, 1);
  
      char keypress;
      bool quit = false;
      int counterCheck = 1;
  
      while (quit == false)
      {
  
  
          // Make an image from the raw capture data
          // Note: cvQueryFrame is a combination of cvGrabFrame and cvRetrieveFrame
          pFrame = cvQueryFrame(pCapture);
  
          // Draw the original frame in our window
          cvShowImage("Live_Cam", pFrame);
  
  
  
  
          shapeDetectionNew(pFrame);
  
  
  
      } // End of while loop
  
      cvDestroyAllWindows();
  
  }
  catch (Exception ex)
  {
  
  }
  
  return 1;
  }
  </code></pre>