本文介绍了OpenCV内存泄漏问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

drawItem的这个函数通过循环被调用多次,我每次被调用时都面临着内存泄漏问题。我想这个问题是由于resizeImage()函数,但我似乎无法确定的问题,这是C ++ \CLI与OpenCV库。

This function of drawItem is called multiple times through a loop, I'm facing a memory leak issue everytime it is called. I'm thinking that the problem is due to the resizeImage() function, but I can't seem to pinpoint the problem, this is C++\CLI with OpenCV library.

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


推荐答案

问题,它是由于多内存分配。

I've found out the problem, it is due to Multiple Memory Allocations.

imgItem指向一些更早的东西,但后我做了
imgItem = resizeImage(imgItem,newItemWidth,newItemHeight, false);

imgItem was pointing to something earlier, but after i didimgItem = resizeImage(imgItem, newItemWidth, newItemHeight, false);

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

imgItem now points to another thing, the memory created in when imgItem was created is lost forever, no variable points to it, hence a memory leak.

所以我使用tempImgItem来解决问题,

so i used tempImgItem to solve the problem,

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

这篇关于OpenCV内存泄漏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 16:50