title: Stack around the variable Opencv & C++
body: <p>I'm working in a project with particle filter. This is a snippet of my code. When the program is executing, when leave this function VS2013 tell me a error: "<strong>Run-Time Check Failure #2 - Stack around the variable 'idx' was corrupted</strong>."
  Do you want any idea what is the problem??</p>
  
  <p>Thanks you!</p>
  
  <p>PS: additional info. If I execute the program in release mode it problem disappear...</p>
  
  <pre><code>void Particles::mergeClusters(cv::Mat image) {
  
      cv::Mat ids_new(0, 0, CV_32SC1);
      cv::Mat means_new(0, 0, CV_64FC1);
      vector&lt;cv::Mat&gt; covs_new(0);
      cv::Mat weights_new(0, 0, CV_64FC1);
  
      cv::Mat ids(0, 0, CV_32SC1);
      cv::Mat means(0, 0, CV_64FC1);
      vector&lt;cv::Mat&gt; covs(0);
      cv::Mat weights(0, 0, CV_64FC1);
  
      // temporary objects used to store non-chosen elements
      // this is the set I in the paper of Vo (Table II)
      clusters.ids.copyTo(ids);
      clusters.means.copyTo(means);
      clusters.covs.swap(covs);
      clusters.weights.copyTo(weights);
  
      int l = 0;
      while (!means.empty()) {
  
          cv::Mat means_t(0, 0, CV_64FC1);
          cv::Mat weights_t(0, 0, CV_64FC1);
          vector&lt;cv::Mat&gt; covs_t(0);
  
          int idx = 0;
          cv::minMaxIdx(weights, NULL, NULL, NULL, &amp;idx);
  
          cv::Mat m_t(0, 0, CV_64FC1);
          cv::Mat w_t(0, 0, CV_64FC1);
          vector&lt;cv::Mat&gt; c_t(0);
  
          vector&lt;int&gt; L(0);
  
          for (int j=0; j&lt;means.rows; j++) {
              cv::Mat u = (means.row(j)-means.row(idx)) * covs[j].inv() * (means.row(j)-means.row(idx)).t();
              if (u.at&lt;double&gt;(0) &lt;= param.U) {
                  m_t.push_back(means.row(j));
                  w_t.push_back(weights.row(j));
                  c_t.push_back(covs[j]);
                  L.push_back(j);
              }
              else {
                  means_t.push_back(means.row(j));
                  weights_t.push_back(weights.row(j));
                  covs_t.push_back(covs[j]);
              }
          }
  
          // Weight
          double W = sum(w_t)[0];
          // Mean
          cv::Mat M(0, 0, CV_64FC1);
          cv::repeat(w_t, 1, m_t.cols, M);
          cv::multiply(M, m_t, M);
          cv::reduce(M, M, 0, CV_REDUCE_SUM);
          cv::divide(M, W, M);
          // Covariance
          cv::Mat C(0, 0, CV_64FC1);
          C = cv::Mat::zeros(covs[0].rows, covs[0].cols, CV_64FC1);
          for (int k=0; k&lt;m_t.rows; k++) {
              C = C + w_t.at&lt;double&gt;(k) * (covs[k] + ((M-m_t.row(k)).t() * (M-m_t.row(k))) );
          }
          cv::divide(C, W, C);
  
          ids_new.push_back(idx);
          means_new.push_back(M);
          covs_new.push_back(C);
          weights_new.push_back(W);
  
          l++;
  
          // update I
          means.release();
          weights.release();
          covs.clear();
  
          means_t.copyTo(means);
          weights_t.copyTo(weights);
          covs_t.swap(covs);
  
          means_t.release();
          weights_t.release();
          covs_t.clear();
      }
  
      ids_new.copyTo(clusters.ids);
      means_new.copyTo(clusters.means);
      weights_new.copyTo(clusters.weights);
      covs_new.swap(clusters.covs);
  }
  </code></pre>