Ask Your Question
-1

Stack around the variable Opencv & C++

asked 2014-11-07 04:45:36 -0600

RiSaMa gravatar image

updated 2014-11-07 05:53:36 -0600

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);
}
edit retag flag offensive close merge delete

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 ( 2014-11-07 06:06:07 -0600 )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 ( 2014-11-07 06:19:19 -0600 )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 ( 2014-11-07 06:27:22 -0600 )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 ( 2014-11-07 06:30:19 -0600 )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 ( 2014-11-11 06:22:34 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-11-12 03:14:57 -0600

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!

edit flag offensive delete link more

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 ( 2014-11-12 14:28:32 -0600 )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 ( 2014-11-13 03:38:22 -0600 )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 ( 2014-11-13 09:00:17 -0600 )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 ( 2014-11-13 09:14:33 -0600 )edit

Question Tools

Stats

Asked: 2014-11-07 04:45:36 -0600

Seen: 1,493 times

Last updated: Nov 12 '14