Search code examples
c++visual-studio-2010opencvsift

Opencv Error: Insufficient memory(Failled to allocated) when I use the sift()


Here is my code:

void Init()  // For the first frame.
{
    Mat in=cv::imread("img1.jpg");
    SIFT* s=NULL;
    vector< cv::KeyPoint > key;key.clear();
    Mat* descriptors=NULL;
    _CrtMemState Sh1,Sh2,Sh_Diff;
    _CrtMemCheckpoint(&Sh1);
    s = new SIFT();
    descriptors = new Mat();
    (*s)(in, Mat(), key, *descriptors);
    delete s;
    _CrtMemCheckpoint(&Sh2);
    _CrtMemDifference(&Sh_Diff, &Sh1, &Sh2); 
    _CrtMemDumpAllObjectsSince(&Sh_Diff);

    GetKCent(*descriptors);key.clear();
    mask=cv::Scalar(0);
    mask(selection)=cv::Scalar(255);
    Mat mask(in.rows,in.cols,CV_8U);
    s=new SIFT();
    if (descriptors!=NULL) delete descriptors;
    descriptors = new Mat();
    (*s)(in,mask,key,*descriptors);
    delete s;
}

The insufficient memory error occured when the program execute this line

(*s)(in,mask,key,*descriptors);

It is the second sift::operator(). I think the memory may leak. Who can help me?


Solution

  • I believe your opencv version is 2.4.4 or later. I encounter the same problem using opencv 2.4.7 and 2.4.8. Then I go back to 2.4.3 and everything goes well. I believe it is a bug with recent opencv.