title: C++: Debug Assertion Failed (using OpenCV library)
body: <p>I have the following code:</p>
  
  <pre><code>int H_MIN = 0;
  int H_MAX = 256;
  int S_MIN = 0;
  int S_MAX = 256;
  int V_MIN = 0;
  int V_MAX = 256;
  
  const int FRAME_WIDTH = 640;
  const int FRAME_HEIGHT = 480;
  
  const int MAX_NUM_OBJECTS = 50;
  
  const int MIN_OBJECT_AREA = 20 * 20;
  const int MAX_OBJECT_AREA = FRAME_HEIGHT*FRAME_WIDTH / 1.5;
  
  const string windowName = "Original Image";
  const string windowName1 = "HSV Image";
  const string windowName2 = "Thresholded Image";
  const string windowName3 = "After Morphological Operations";
  const string trackbarWindowName = "Trackbars";
  void on_trackbar(int, void*)
  {
  
  
  }
  string intToString(int number) {
  
  
      std::stringstream ss;
      ss &lt;&lt; number;
      return ss.str();
  }
  void createTrackbars() {
  
  
      namedWindow(trackbarWindowName, 0);
      char TrackbarName[50];
      sprintf(TrackbarName, "H_MIN", H_MIN);
      sprintf(TrackbarName, "H_MAX", H_MAX);
      sprintf(TrackbarName, "S_MIN", S_MIN);
      sprintf(TrackbarName, "S_MAX", S_MAX);
      sprintf(TrackbarName, "V_MIN", V_MIN);
      sprintf(TrackbarName, "V_MAX", V_MAX); 
      createTrackbar("H_MIN", trackbarWindowName, &amp;H_MIN, H_MAX, on_trackbar);
      createTrackbar("H_MAX", trackbarWindowName, &amp;H_MAX, H_MAX, on_trackbar);
      createTrackbar("S_MIN", trackbarWindowName, &amp;S_MIN, S_MAX, on_trackbar);
      createTrackbar("S_MAX", trackbarWindowName, &amp;S_MAX, S_MAX, on_trackbar);
      createTrackbar("V_MIN", trackbarWindowName, &amp;V_MIN, V_MAX, on_trackbar);
      createTrackbar("V_MAX", trackbarWindowName, &amp;V_MAX, V_MAX, on_trackbar);
  }
  void drawObject(int x, int y, Mat &amp;frame) {
  
      circle(frame, Point(x, y), 20, Scalar(0, 255, 0), 2);
      if (y - 25&gt;0)
          line(frame, Point(x, y), Point(x, y - 25), Scalar(0, 255, 0), 2);
      else line(frame, Point(x, y), Point(x, 0), Scalar(0, 255, 0), 2);
      if (y + 25&lt;FRAME_HEIGHT)
          line(frame, Point(x, y), Point(x, y + 25), Scalar(0, 255, 0), 2);
      else line(frame, Point(x, y), Point(x, FRAME_HEIGHT), Scalar(0, 255, 0), 2);
      if (x - 25&gt;0)
          line(frame, Point(x, y), Point(x - 25, y), Scalar(0, 255, 0), 2);
      else line(frame, Point(x, y), Point(0, y), Scalar(0, 255, 0), 2);
      if (x + 25&lt;FRAME_WIDTH)
          line(frame, Point(x, y), Point(x + 25, y), Scalar(0, 255, 0), 2);
      else line(frame, Point(x, y), Point(FRAME_WIDTH, y), Scalar(0, 255, 0), 2);
  
      putText(frame, intToString(x) + "," + intToString(y), Point(x, y + 30), 1, 1, Scalar(0, 255, 0), 2);
  
  }
  void morphOps(Mat &amp;thresh) {
  
      Mat erodeElement = getStructuringElement(MORPH_RECT, Size(3, 3));
      Mat dilateElement = getStructuringElement(MORPH_RECT, Size(8, 8));
      erode(thresh, thresh, erodeElement);
      erode(thresh, thresh, erodeElement);
      dilate(thresh, thresh, dilateElement);
      dilate(thresh, thresh, dilateElement);
  }
  void trackFilteredObject(int &amp;x, int &amp;y, Mat threshold, Mat &amp;cameraFeed) {
      Mat temp;
      threshold.copyTo(temp);
  
      vector&lt; vector&lt;Point&gt; &gt; contours;
      vector&lt;Vec4i&gt; hierarchy;
      findContours(temp, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
      double refArea = 0;
      bool objectFound = false;
      if (hierarchy.size() &gt; 0) {
          int numObjects = hierarchy.size();
          if (numObjects&lt;MAX_NUM_OBJECTS) {
              for (int index = 0; index &gt;= 0; index = hierarchy[index][0]) {
  
                  Moments moment = moments((cv::Mat)contours[index]);
                  double area = moment.m00;
                  if (area&gt;MIN_OBJECT_AREA &amp;&amp; area&lt;MAX_OBJECT_AREA &amp;&amp; area&gt;refArea) {
                      x = moment.m10 / area;
                      y = moment.m01 / area;
                      objectFound = true;
                      refArea = area;
                  }
                  else objectFound = false;
  
              }
              //let user know you found an object
              if (objectFound == true) {
                  putText(cameraFeed, "Tracking Object", Point(0, 50), 2, 1, Scalar(0, 255, 0), 2);
  
                  drawObject(x, y, cameraFeed);
              }
  
          }
          else putText(cameraFeed, "TOO MUCH NOISE! ADJUST FILTER", Point(0, 50), 1, 2, Scalar(0, 0, 255), 2);
      }
  }
  int main(int argc, char* argv[])
  {
          bool trackObjects = true;
      bool useMorphOps = false;
      Mat cameraFeed;
      Mat HSV;
      Mat threshold;
      int x = 0, y = 0;
      createTrackbars();
      VideoCapture capture;
      capture.open(0);
      capture.set(CV_CAP_PROP_FRAME_WIDTH, FRAME_WIDTH);
      capture.set(CV_CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT);
      while (1) {
          capture.read(cameraFeed);
          cvtColor(cameraFeed, HSV, COLOR_BGR2HSV);
          inRange(HSV, Scalar(H_MIN, S_MIN, V_MIN), Scalar(H_MAX, S_MAX, V_MAX), threshold);
          if (useMorphOps)
              morphOps(threshold);
          if (trackObjects)
              trackFilteredObject(x, y, threshold, cameraFeed);
          imshow(windowName2, threshold);
          imshow(windowName, cameraFeed);
          imshow(windowName1, HSV);
          waitKey(30);
      }
      return 0;
  }
  </code></pre>
  
  <p>Whenever i run it in debug mode, i get the following error:
  Debug Assertion Failed!
  Expression: __acrt_first_block == header</p>
  
  <p>I have to say that it only happens when the boolean variable trackObjects in the main function is equal to true (when it's set to false the program works great). </p>
  
  <p>Setting it to true "activates" this part of the code in the main function:</p>
  
  <pre><code>    if (trackObjects)
          trackFilteredObject(x, y, threshold, cameraFeed);
  </code></pre>
  
  <p>which might be causing the problem. </p>
  
  <p>Any idea on how i can fix this?
  Thanks!</p>