2011-04-10 154 views
1

這個drawItem函數通過一個循環被多次調用,每次調用它時都會遇到內存泄漏問題。我在想這個問題是由resizeImage()函數引起的,但我似乎無法找到問題所在,這是C++ \ CLI與OpenCV庫的關係。OpenCV內存泄漏問題

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

回答

3

我發現了這個問題,這是由於多種內存分配outImage。 imgItem是指向之前的東西,但是在我做了之後。

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

我想這是因爲你本身就是所說的「resizeImage」,你忘了釋放內部

+0

我試圖把一個cvReleaseImage(&imgItem);在IplImage * outImg = 0之後;但它仍然泄漏內存:/ – Qmage 2011-04-12 12:04:27

+0

我的意思是(&outImg)。我也不能把cvReleaseImage(&outImg)在返回之前,程序會崩潰 – Qmage 2011-04-12 12:10:36

+0

我相信你是正確的,我必須釋放outImg,因爲我用cvCreateImage創建它,它必須被釋放。但現在的問題是我如何釋放它,因爲我需要「返回」它:/ – Qmage 2011-04-12 12:28:35