2012-06-22 44 views
0

這是代碼此代碼中的內存泄漏C++ opencv?

CvMemStorage *mem123 = cvCreateMemStorage(0); 
CvSeq* ptr123;CvRect face_rect123; 
CvHaarClassifierCascade* cascade123 = (CvHaarClassifierCascade*)cvLoad("haarcascade_frontalface_alt2.xml"); //detects the face if it's frontal 
void HeadDetection(IplImage* frame,CvRect* face){ 
    ptr123=cvHaarDetectObjects(frame,cascade123,mem123,1.2,2,CV_HAAR_DO_CANNY_PRUNING); 
    if(!ptr123){return ;} 
    if(!(ptr123->total)){return ;} 
    face_rect123=*(CvRect*)cvGetSeqElem(ptr123, 0); //CvRect face_rect holds the position of Rectangle 
    face->height=face_rect123.height; 
    face->width=face_rect123.width; 
    face->x=face_rect123.x; 
    face->y=face_rect123.y; 
    return ; 
}//detects the position of head and it is fed in CvRect*face as rectangle 
int main(){ 
    IplImage* oldframe=cvCreateImage(cvSize(640,480),8,3); 
    CvCapture* capture=cvCaptureFromCAM(CV_CAP_ANY); 
    CvRect a;a.height=0;a.width=0;a.x=0;a.y=0; 
    while(1){ 

     oldframe=cvQueryFrame(capture); //real frame captured of size 640x480 
     cvFlip(oldframe,oldframe,1); 
     cvResize(oldframe,frame); //frame scaled down 4 times 
     HeadDetection(frame,&a); 
     cvShowImage("frame",frame); 
     cvWaitKey(1); 
    } 
} 

在這裏,如果 「HeadDetection(幀,&一個);」被評論,然後使用任務管理器,我看到angledetection.exe(我的項目的名稱)消耗20188 Kb內存(然後沒有發生內存泄漏)。

但是,如果我不發表意見的任務管理器顯示,一些內存泄漏發生的事情(約300KB/s)的

我使用VS 2010的64位Windows 7位操作系統(酷睿2)。

此代碼試圖臉檢測和OpenCV的2.1

由哈爾檢測得到正方形的四個角如果有不清楚的地方請詢問。 :-)

在此先感謝。

+0

我試圖使用一些內存泄漏工具,但我無法獲得任何進展(總之它是一個小代碼) – Michael

回答

1

當您撥打cvHaarDetectObjects時,您會看到一個指向對象的指針。

但是你從來沒有釋放它(的對象ptr123點)。

另外face_rect123未被釋放。

順便說一句,你應該考慮重構代碼並給變量賦予更好的名字。

+0

:如何釋放ptr123? – Michael