關於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),在此輸入圖片描述測試

這是inspector 執行的結果。this

若是將上述cvShowImage 註釋掉以後,結果以下: 在此輸入圖片描述code

cvshowimage 是否內存泄露,有待研究。圖片

其二: 在國外的論壇找到了一個,由數組轉換成IplImage 結構時,須要注意內存泄露的問題:問題及測試代碼以下:內存

<!-- 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 <opencv/cv.h> #include <stdlib.h> #define NLOOPS 1000 int main(void){ int i,jget

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 ){it

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

}io

free(buff); return 0; }opencv

若是採用註釋的寫法,就會有內存泄露,im->imageData = buff 比 cvSetData 要好

相關文章
相關標籤/搜索