Stack around the variable Opencv & C++
Hi! 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: "Run-Time Check Failure #2 - Stack around the variable 'idx' was corrupted." Do you want any idea what is the problem??
Thanks you!
PS: additional info. If I execute the program in release mode it problem disappear...
void Particles::mergeClusters(cv::Mat image) {
cv::Mat ids_new(0, 0, CV_32SC1);
cv::Mat means_new(0, 0, CV_64FC1);
vector<cv::Mat> 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<cv::Mat> 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<cv::Mat> covs_t(0);
int idx = 0;
cv::minMaxIdx(weights, NULL, NULL, NULL, &idx);
cv::Mat m_t(0, 0, CV_64FC1);
cv::Mat w_t(0, 0, CV_64FC1);
vector<cv::Mat> c_t(0);
vector<int> L(0);
for (int j=0; j<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<double>(0) <= 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<m_t.rows; k++) {
C = C + w_t.at<double>(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);
}
" Stack around the variable 'idx' was corrupted." - this is only the outcome of the heap corruption. the reason might be anywhere in your program (not nessecarily in the code you show)
Thanks you very much for your answer!! The error appear when the program finish this function... Then the problem could is in everywhere in THIS function, no? This code (complete code) is working properly in a Mac... I don't understand why it fail in Windows (VS2013)... Thanks you!!
"If I execute the program in release mode it problem disappear.."
" is working properly in a Mac"
that you don't see it in release or on the mac , does not at all mean there is no problem.
unfortunately, your 'snippet' is far too large to solve it by 'staring at it', but too incomplete to run a real test.
can you maybe put up the whole code somewhere ?
How can I share my code with you? I;m going crazy... I can;t found the error!!
THANKS YOU FOR YOUR HELP!!