Reputation: 5354
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?
Upvotes: 2
Views: 963
Reputation: 494
I was able to find the memory leaks with cv::VideoCapture
and NSAutoreleasePool
. The problems are all in modules/highgui/src/cap_qtkit.mm
.
I found about 3 examples of the following:
NSAutoreleasePool
is allocated.return
So the fix is for every occurrence of [[NSAutoreleasePool alloc] init]
, check the remainder of the function for a return
. If there isn't a statement along the lines of [localpool drain]
(or whatever variable name was used in the alloc) just prior to the return
, then add one in.
There are 2 other source files in highgui that use NSAutoreleasePool
that may have similar issues, but I don't use them and haven't checked.
Once I fixed this issue, my memory leaks went away. Again, I'm using OpenCV 2.4.2.
Upvotes: 0
Reputation: 5354
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.
Upvotes: 1
Reputation: 1397
i think the problem is this part
while (1)
{
frame = cvQueryFrame (capture);
cvShowImage ("Output", frame);
}
in my idea you have to change this unlimited WHILE to FOR and for have to be fix memory size and after for you have to use
Release();
or
EndQuery();
i do this method like this
while (1)
{
for(int i=0;i<x;++i)
{
frame = cvQueryFrame (capture);
cvShowImage ("Output", frame);
}
Release();
EndQuery();
}
Upvotes: 2