堆损坏错误;无法释放cv :: Mat opencv

时间:2012-05-23 08:18:50

标签: c++ opencv merge

此代码可以正常工作直到最后一行。它将正确的图像保存在磁盘上但在退出函数后显示“内存泄漏” - 堆损坏。我读过Mat不需要明确发布。在我的情况下,它会在释放和w / o释放时崩溃。请帮忙。

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

2 个答案:

答案 0 :(得分:1)

有了这些小信息,我只能给你一些支持,但没有真正的解决方案:

1。)我猜您使用的是Win7。所以请visit Dr. Memory并安装它(将路径添加到系统变量中)。然后,您可以使用以下命令启动应用程序:drmemory.exe -no_follow_children C:\\the_path\\YourExecutable.exe argv[1] ... argv[n]。 -no_follow_children用于忽略其他第三方代码。运行此命令。记忆博士将结果写为C:\Users\NAMEHERE\AppData\Roaming\Dr. Memory\。看一下,也许你现在有一个提示。如果不是 - &gt;把它贴在这里。 =)

2。)在没有压缩参数的情况下编写图像时,OpenCV有时会崩溃。我经历过这段时间,所以我总是给imwrite函数提供一个int参数向量:

vector<int> crparam;
crparam.push_back(CV_IMWRITE_PNG_COMPRESSION);
cv::imwrite("D:\\testAfterCannyEdgeDetetionFilter.png", mixedBandsMat, crparam);

答案 1 :(得分:-2)

我在STL for C ++中找不到vector [i] .release成员函数。尝试vector [i] .erase