memory-leaks - OpenCV内存泄漏问题

标签 memory-leaks opencv c++-cli

drawItem的这个函数在循环中被多次调用,每次调用都会遇到内存泄漏的问题。我认为问题是由于 resizeImage() 函数引起的,但我似乎无法查明问题所在,这是带有 OpenCV 库的 C++\CLI。

drawItem()
{
imgItem = resizeImage(imgItem, newItemWidth, newItemHeight, false);
imgMask = resizeImage(imgMask, newItemWidth, newItemHeight, false);
cvSetImageROI(image3, cvRect(x1,y1,newItemWidth, newItemHeight));
cvCopy(imgItem, image3, imgMask);
cvResetImageROI(image3);
cvReleaseImage( &imgItem );
cvReleaseImage( &imgMask );
}

IplImage* resizeImage(const IplImage *origImg, int newWidth, int newHeight, bool keepAspectRatio)
{
    IplImage *outImg = 0;
    int origWidth;
    int origHeight;
    if (origImg) {
        origWidth = origImg->width;
        origHeight = origImg->height;
    }
    if (newWidth <= 0 || newHeight <= 0 || origImg == 0
        || origWidth <= 0 || origHeight <= 0) {
        //cerr << "ERROR: Bad desired image size of " << newWidth
        //  << "x" << newHeight << " in resizeImage().\n";
        exit(1);
    }

    if (keepAspectRatio) {
        // Resize the image without changing its aspect ratio,
        // by cropping off the edges and enlarging the middle section.
        CvRect r;
        // input aspect ratio
        float origAspect = (origWidth / (float)origHeight);
        // output aspect ratio
        float newAspect = (newWidth / (float)newHeight);
        // crop width to be origHeight * newAspect
        if (origAspect > newAspect) {
            int tw = (origHeight * newWidth) / newHeight;
            r = cvRect((origWidth - tw)/2, 0, tw, origHeight);
        }
        else {  // crop height to be origWidth / newAspect
            int th = (origWidth * newHeight) / newWidth;
            r = cvRect(0, (origHeight - th)/2, origWidth, th);
        }
        IplImage *croppedImg = cropImage(origImg, r);

        // Call this function again, with the new aspect ratio image.
        // Will do a scaled image resize with the correct aspect ratio.
        outImg = resizeImage(croppedImg, newWidth, newHeight, false);
        cvReleaseImage( &croppedImg );

    }
    else {

        // Scale the image to the new dimensions,
        // even if the aspect ratio will be changed.
        outImg = cvCreateImage(cvSize(newWidth, newHeight),
            origImg->depth, origImg->nChannels);
        if (newWidth > origImg->width && newHeight > origImg->height) {
            // Make the image larger
            cvResetImageROI((IplImage*)origImg);
            // CV_INTER_LINEAR: good at enlarging.
            // CV_INTER_CUBIC: good at enlarging.           
            cvResize(origImg, outImg, CV_INTER_LINEAR);
        }
        else {
            // Make the image smaller
            cvResetImageROI((IplImage*)origImg);
            // CV_INTER_AREA: good at shrinking (decimation) only.
            cvResize(origImg, outImg, CV_INTER_AREA);
        }

    }
    return outImg;
}

IplImage* cropImage(const IplImage *img, const CvRect region)
{
    IplImage *imageCropped;
    CvSize size;

    if (img->width <= 0 || img->height <= 0
        || region.width <= 0 || region.height <= 0) {
        //cerr << "ERROR in cropImage(): invalid dimensions." << endl;
        exit(1);
    }

    if (img->depth != IPL_DEPTH_8U) {
        //cerr << "ERROR in cropImage(): image depth is not 8." << endl;
        exit(1);
    }

    // Set the desired region of interest.
    cvSetImageROI((IplImage*)img, region);
    // Copy region of interest into a new iplImage and return it.
    size.width = region.width;
    size.height = region.height;
    imageCropped = cvCreateImage(size, IPL_DEPTH_8U, img->nChannels);
    cvCopy(img, imageCropped);  // Copy just the region.

    return imageCropped;
}

最佳答案

我发现了问题,这是由于多重内存分配造成的。

imgItem 之前指向了一些东西,但在我做了之后 imgItem = resizeImage(imgItem, newItemWidth, newItemHeight, false);

imgItem 现在指向另一件事,创建 imgItem 时创建的内存永远丢失,没有变量指向它,因此内存泄漏。

所以我用tempImgItem来解决这个问题,

tempImgItem = resizeImage(imgItem, newItemWidth, newItemHeight, false);
tempImgMask = resizeImage(imgMask, newItemWidth, newItemHeight, false);
cvReleaseImage( &imgItem );
cvReleaseImage( &imgMask );
imgItem = tempImgItem;
imgMask = tempImgMask;
tempImgItem = NULL;
tempImgMask = NULL;

关于memory-leaks - OpenCV内存泄漏问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5610294/

相关文章:

multithreading - Delphi XE +线程+ idHttp没有内存泄漏

ios - AVAssetWriter 东西泄露

c++ - 当结构对象超出范围时,如何删除结构对象内的指针?

c# - Boost 数学(ibeta_inv 函数)不是线程安全的?

swift - 为什么在使用 ARSCNView 关闭 ViewController 时 UI 会卡住?

c++ - 如何在 OpenCV 中获取旋转矩形的顶点?

python - 使用 Python 在 OpenCV 中检测线条和形状

python-3.x - 如何找到轮廓的坐标并进行裁剪?

interop - 如何将 C++/CLI 代码的某些部分编译为 native 代码,将某些部分编译为托管代码?

c# - 固定一个函数指针