Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

heap corruption error; unable to release cv::Mat opencv

Tags:

c++

merge

opencv

This code works fine till the last line. It saves the correct image on the disk but it shows a "memory leak" after going out of the function - heap corruption. I had read that Mat doesn't need to be explicitly released. In my case, it crashes both with releasing and w/o releasing. Please help.

void CannyEdgeDetectionFilter::applyFilter(Mat& mat, Mat& mixedBandsMat)
{
    //Mat mixedBandsMat;

    vector<Mat> bandWiseImages;
    split(mat, bandWiseImages);

    //! Evaluate numChannels to be filtered in the input image
    int numChannels = mat.channels();
    int type = mat.type();

    //! Multiplied by 8 to get bits from Bytes
    int singleChannelDepth = 8*mat.elemSize1();

    for (int i = 0; i < numChannels; i++)
    {
        Canny(bandWiseImages[i], bandWiseImages[i], m_LowerThreshold,
            m_UpperThreshold, m_Kernel.rows);
    }

    //! Creating filteredImgMat in order to set DataValues
    mixedBandsMat.create(mat.rows, mat.cols, mat.type());

    //! Unifying the channels back to the output image
    merge(bandWiseImages, mixedBandsMat);
#if 1
    //Release bandWiseImages Mat memory
    int bandWiseVecSize = bandWiseImages.size();
    for(int i = 0; i < bandWiseVecSize; i++)
        bandWiseImages[i].release();
    bandWiseImages.clear();
    //fromTo.clear();
#endif
    imwrite("D:\\testAfterCannyEdgeDetetionFilter.jpg", mixedBandsMat);
    mixedBandsMat.release();
}
like image 293
user1412066 Avatar asked Nov 27 '22 10:11

user1412066


1 Answers

With this little information I can only give you some support but no real solution:

1.) I guess that you are using Win7. So please visit Dr. Memory and install it (with path added to the system variables). Then you can start you application using the following command: drmemory.exe -no_follow_children C:\\the_path\\YourExecutable.exe argv[1] ... argv[n]. The -no_follow_children is used to ignore other third-party-code. Run this command. Dr. Memory will write the result down to C:\Users\NAMEHERE\AppData\Roaming\Dr. Memory\. Take a look, perhaps you now have a hint. If not -> post it here. =)

2.) OpenCV can sometimes crash when you are writing an image without compression-parameter. I've experienced this some time so I always give a vector of int-parameters to the imwrite-function:

vector<int> crparam;
crparam.push_back(CV_IMWRITE_PNG_COMPRESSION);
cv::imwrite("D:\\testAfterCannyEdgeDetetionFilter.png", mixedBandsMat, crparam);
like image 145
Mr.Mountain Avatar answered Dec 04 '22 05:12

Mr.Mountain