简体   繁体   中英

Program crashes on using grabCut in OpenCV 2.3.1

I am using the following code :

#include <iostream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

    int main(int argc, char* argv[])
    {
        const string ipImgName= argv[1];
        Mat ipImg;
        ipImg = imread( ipImgName, 1 );

        cv::Mat gcImg;
        cv::Mat bgdModel;
        cv::Mat fgdModel;
        cv::Rect rect(0, 0, ipImg.cols-1, ipImg.rows-1);
        cv::grabCut( ipImg, gcImg, rect, bgdModel, fgdModel, 1, cv::GC_INIT_WITH_RECT ); 


        return 0;
    }

But when exiting the main loop, when the debugger goes into the ~Mat() destructor, the code crashes on release() ( saying "this maybe due to a corruption of the heap" ) for gcImg or bgdModel or fgdModel.

However, if I allocate the cv::Mat s on heap using new and then don't delete them after, the code runs just fine. I am using Visual Studio 2010 with OpenCV 2.3.1.

If you're running a Debug build that's linked with the Release libs, you can get this spurious "error" reported due to the different memory allocation routines used.

In Debug builds, memory allocations are protected with guard bytes at either end of the allocated blocks and various checks are performed for corruption, leaking etc. In Release builds these checks are not performed and when you mix Debug and Release code so that memory allocated with one is freed with the other, this is typical of the error you see.

It's not actually a real error, just a side effect of mixing different runtimes. MSDN has more details .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM