I have a memory leak in my OpenCV application. Its a medium size application with a dozon of classes and a few thousands lines of code. Somehow, I managed to produce a large
Not an answer, but a suggestion: Move from OpenCV C interface to C++. If properly used, it will minimize your chances for a leak, now and in the future. Its smart pointers embedded in the objects automatically free memory.
In the worst case, you'll have a performance penalty (too many allocs/deallocs), but those are easy to spot in a profiler.
The C++ interface is using
Mat intead of IplImage,
Point instead of CvPoint,
cv::function() instead of cvFunction.
And you do not have to declare pointers to images:
Mat src = imread("myfile.jpg");
Mat gray; // note that I do not allocate it.
// This is done automatically in the next functions
cv::cvtColor(src, gray, CV_BGR2GRAY);
imshow("Gray image", gray);
waitKey();
If you have some legacy code, or a third-party that uses the other interface, it's easy to convert back and forth:
Mat src(width, height, CV_8UC3);
IplImage* legacyImg;
legacyImg = &(IplImage)src;
Other datatypes (like CvPoint
) are automatically converted. CvSeq
is replaced by std::vector<T>