First time here? Check out the FAQ!

Ask Your Question
-1

Stack around the variable Opencv & C++

asked Nov 7 '14

RiSaMa gravatar image

updated Nov 7 '14

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);
}
Preview: (hide)

Comments

1

" 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)

berak gravatar imageberak (Nov 7 '14)edit

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!!

RiSaMa gravatar imageRiSaMa (Nov 7 '14)edit

"If I execute the program in release mode it problem disappear.."

" is working properly in a Mac"

  • you should be grateful for VS showing you a problem in debug mode.

that you don't see it in release or on the mac , does not at all mean there is no problem.

berak gravatar imageberak (Nov 7 '14)edit

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 ?

berak gravatar imageberak (Nov 7 '14)edit

How can I share my code with you? I;m going crazy... I can;t found the error!!

THANKS YOU FOR YOUR HELP!!

RiSaMa gravatar imageRiSaMa (Nov 11 '14)edit

1 answer

Sort by » oldest newest most voted
0

answered Nov 12 '14

RiSaMa gravatar image

Finally I could solve the problem.

I needed declare dinamically the int:

int *idx = nullptr;  // new pointer declared
idx = new int();    // memory dynamically allocated

Now it's working propertly!

Preview: (hide)

Comments

Even more simple on one line: int* idx = new int; Also don't forget to delete your dynamically allocated int with: delete idx;

boaz001 gravatar imageboaz001 (Nov 12 '14)edit

Another problem :'(... If I don't release (delete) the memory dinamically allocated --> work fine.. If I use delete idx --> CRASH: "HEAP CORRUPTION DETECTED: after normal block. CRT detected that the application wrote to memory after end of heap buffer"... What you recommended me to do?

THANKS YOU!

RiSaMa gravatar imageRiSaMa (Nov 13 '14)edit

Here's why I already had my doubts that you actually solved it with dynamically allocating idx. It is most likely not another problem, not deleting probably just hid the problem so I have my doubts about it 'working fine'. Like @berak already said. The actual reason for the heap corruption can be anywhere. You stated that you run it on a mac too, I suggest you run your program through valgrind, try to minimize the code to the minimum and debug, debug, debug...

boaz001 gravatar imageboaz001 (Nov 13 '14)edit

doubtful, that you hit the right spot there. int *idx = new int; will indeed put the memory location of idx to another place in memory, but i bet a beer, that the original location will still get overwritten , and now you just don't see it anymore.

berak gravatar imageberak (Nov 13 '14)edit

Question Tools

Stats

Asked: Nov 7 '14

Seen: 1,568 times

Last updated: Nov 12 '14