It turns out that using cvCaptureFromCAM creates a memory in initialization. Usage of cvCaptureFromCAM creates a 48 byte memory leak of a NSAutoreleasePool object. I initially thought my program had a bug, but when replaced with a much simpler program, the memory leak still occurs.
For example:
#include <opencv2/opencv.hpp>
int main()
{
IplImage *frame = 0;
CvCapture *capture = cvCaptureFromCAM (-1); //leak occurs on this line
//using 0 instead of -1 creates a leak too
cvNamedWindow ("Output", CV_WINDOW_AUTOSIZE);
while (1)
{
frame = cvQueryFrame (capture);
cvShowImage ("Output", frame);
}
cvDestroyAllWindows ();
cvReleaseCapture (&capture);
return 0;
}
I am using Xcode Leaks to find memory leaks and am obviously using the Xcode IDE on Mac OS X 10.6. Does anyone know a solution to the leak? Am I doing something wrong or it a bug in OpenCV or a problem with my computer? I double checked my code and the OpenCV code but couldn’t really find a problem. The memory leak is a one time thing – it doesn’t keep building. However I am not comfortable with leaving ANY leaks in my program. Does anyone have any suggestions?
I was unfortunately unable to find the leak in cvCaptureFromCAM, so I switched to the C++ interface and used a VideoCapture object, which appears to be free of leaks.