2011-04-10 5 views
1

이 drawItem 함수는 루프를 통해 여러 번 호출됩니다. 호출 될 때마다 메모리 누수 문제가 발생합니다. 문제는 resizeImage() 함수로 인한 것 같아요,하지만 문제를 정확히 지적 할 수 없습니다,이 OpenCV 라이브러리 C++ \ CLI 것입니다.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 = resizeImage (imgItem, newItemWidth, newItemHeight, false);

imgItem은 이제 imgItem이 생성되었을 때 생성 된 메모리가 영원히 손실되고 변수가 가리키는 점이 없으므로 메모리 누수가 있습니다.

그래서 난 (imgItem을)를 cvReleaseImage 바꾸어 시도

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

를 문제를 해결하기 위해 사용 tempImgItem; IplImage * outImg = 0 후에; 하지만 여전히 메모리가 누수되고 있습니다 :/ – Qmage

+0

뜻 (& outImg). 또한 반환 전에 바로 cvReleaseImage (& outImg)를 넣을 수 없습니다. 프로그램이 중단됩니다. – Qmage

+0

나는 cvCreateImage로 생성했기 때문에 outImg를 릴리스해야한다고 생각합니다. 하지만 문제는 지금 내가 그것을 "반환"해야하기 때문에 어떻게 풀어야합니까?/ – Qmage