Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

VideoCapture::set() memory leak

I am trying to capture video with a Microsoft LiveCam Studio webcam using OpenCV 2.4.9 (which will use DShow to connect to the camera). Since I want to be able to change camera settings on the fly, I inserted a call to VideoCapture::set() inside the capture loop. This way, as soon as a setting is changed (for example using an OpenCV TrackBar), the camera will be adjusted accordingly.

The issue is that VideoCapture::set() seems to introduce a memory leak, which becomes apparent when put into a loop. When running the code below, memory usage increases significantly during program execution. When disabling the call to VideoCpture::set(), memory usage is constant during execution.

I tested this on OpenCV 2.4.4 and 2.4.9 and both show the same issue (compiled in 32 bit on a Windows 7 x64 machine using VS2010). Changing different camera settings seems to result in different sizes of leaks, but each call seems to produce a leak. e.g. CV_CAP_PROP_FOCUS seems to leak almost 2 MB/second while CV_CAP_PROP_CONTRAST only leaks about 15 KB/second. Unfortunately, VisualLeakDetector does not detect a leak when running this code, making debugging hard.

#include "opencv2/highgui/highgui.hpp"
int main(int argc, char ** argv)
    cv::Mat img;
    cv::VideoCapture video_cap(0);
    for(int i = 0; i < 1000; ++i)
        video_cap.set(CV_CAP_PROP_FOCUS, 14);
        video_cap >> img;
    return 0;