关于cvShowImage中的memory leak 问题

<!-- lang: cpp -->
void test()

{

IplImage* img = cvLoadImage("E:\\CarPlateTest\\3.jpg",0);
cvThreshold(img,img,255,255,CV_THRESH_BINARY_INV | CV_THRESH_OTSU);
cvShowImage("thresh",img);
cvSaveImage("E:/1.jpg",img);
cvWaitKey(10);
cvReleaseImage(&img);

}

对该段代码 使用Intel的Inspector对程序进行分析,发现在使用cvShowImage后 就会出现内存泄漏的情况 (使用的版本为opencv 2.4.5),关于cvShowImage中的memory leak 问题_第1张图片

这是inspector 执行的结果。

如果将上述cvShowImage 注释掉之后,结果如下:
关于cvShowImage中的memory leak 问题_第2张图片

cvshowimage 是否内存泄露,有待研究。

其二:

<!-- lang: cpp -->

<!-- lang: cpp -->
That's strange. As far as I know, cvReleaseImage released both the image header and the image data. I did the piece of code below and in this certain example, cvReleaseImage does not free the buffer that contains the data. There I didn't use cvSetData but I just updated the pointer to the image data. If you uncomment the commented lines and comment the ones just below each one, program still runs but you'll get some memory leaks. I used OpenCV 2.2 (this is the legacy interface).

include

include

define NLOOPS 1000

int main(void){
int i,j

char buff = (char ) malloc( sizeof(char) * 3 * 640 * 480 );
for( i = 0; i < 640 * 480 * 3; i++ ) buff[i] = 128;
j = 0;
while( j++< NLOOPS ){

  IplImage *im = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U, 3); 
  //cvSetData(im, buff, im->widthStep);  ---> If you use that version you'll get memory  leaks. Comment line below.
  im->imageData = buff;
  cvWaitKey(4);
  cvShowImage("kk", im);
  //cvReleaseImageHeader(&im);           ---> If you use that version you'll get memory leaks. Comment line below.
  cvReleaseImage(&im);

  free(im);

}

free(buff);
return 0;
}

如果采用注释的写法,就会有内存泄露,im->imageData = buff 比 cvSetData 要好

你可能感兴趣的:(关于cvShowImage中的memory leak 问题)