Ask Your Question
1

VideoCapture::set() memory leak

asked 2014-06-24 10:55:56 -0600

mojoritty gravatar image

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;
        cv::imshow("image",img);
        cv::waitKey(10);
    }
    return 0;
}
edit retag flag offensive close merge delete

Comments

+1 for the issue !

berak gravatar imageberak ( 2014-06-25 05:10:58 -0600 )edit

2 answers

Sort by » oldest newest most voted
2

answered 2014-06-27 07:16:24 -0600

mojoritty gravatar image

I seem to have found a solution to this issue and reported it as a bug in the bugtracker. It turns out the memory leak originates in videoInput::setVideoSettingCamera(), where an IBaseFilter object is created in a call to getDevice() but never released. I presented a solution in the bugreport.

This solves the memory leak for the largest part. However, it seems there is still a very small leak, several bytes per call probably, that originates from the call to getDevice(). Unfortunately, I have not been able to trace this bug to its origin, and was not able to solve it. Maybe someone else is able to locate it, I suspect the calls to CoCreateInstance and CreateClassEnumerator cause the leak, since memory usage seems to increase after these calls.

edit flag offensive delete link more

Comments

Fill free to contribute your fix to OpenCV via pull request on Github: http://code.opencv.org/projects/opencv/wiki/How_to_contribute.

Alexander Smorkalov gravatar imageAlexander Smorkalov ( 2014-06-28 12:02:11 -0600 )edit
0

answered 2014-06-24 11:21:08 -0600

Haris gravatar image

updated 2014-06-24 11:37:15 -0600

Probably the line video_cap.set(CV_CAP_PROP_FOCUS, 14) inside the loop cause the memory leak, here you need to set the capture property only at once like,

cv::Mat img;
cv::VideoCapture video_cap(0);
video_cap.set(CV_CAP_PROP_FOCUS, 14);

for(int i = 0; i < 1000; ++i)
{
    video_cap >> img;
    cv::imshow("image",img);
    cv::waitKey(10);
}
edit flag offensive delete link more

Comments

As I tried to explain in my post, I want to use some controls to adjust the camera settings during recording. In that case, the value passed to VideoCapture::set() would be a variable (i.e. video_cap.set(CV_CAP_PROP_FOCUS, new_focus);), and the variable might be adjusted during program execution, for example using an OpenCV TrackBar (e.g. cv::createTrackBar("bar","window",new_focus,30);). When the camera setting is only applied once, no runtime changes are possible. In any case, moving the line out of the loop is only a work-around. In my view, the command should not leak memory in any case.

mojoritty gravatar imagemojoritty ( 2014-06-24 13:38:09 -0600 )edit
1

Check the answer here http://answers.opencv.org/question/30811/what-are-the-default-webcam-settings/#30817 and let me know if there are some leak.

Haris gravatar imageHaris ( 2014-06-24 23:05:12 -0600 )edit

I was considering this kind of workaround. However, this would not fix the leak, only make it less of an issue since it does not leak every loop iteration. When the user makes many adjustments, significant amounts of memory will still be leaked. I still think this is an issue that should better be fixed. Unless this is intended behavior, in which case it would be good to document this (and explain why this is the case).

mojoritty gravatar imagemojoritty ( 2014-06-25 02:40:42 -0600 )edit

btw where did you find the property_id CV_CAP_PROP_FOCUS I can’t find on documentation.

Haris gravatar imageHaris ( 2014-06-25 04:14:38 -0600 )edit

The documentation is pretty much incomplete to that respect. The properties are defined in highgui_c.h, and there are many more than the ones listed in the documentation. Easiest way is to look in the VS Intellisense auto-complete list, that's how I found them.

mojoritty gravatar imagemojoritty ( 2014-06-25 04:19:57 -0600 )edit

As I haven’t works with CV_CAP_PROP_FOCUS yet, I can’t help you much, and If you think it's bug, you can report here

Haris gravatar imageHaris ( 2014-06-25 04:30:38 -0600 )edit

I am having trouble with a memory leak without the ::set() !!! What is the proper way to handle reading video?

a_w_baker gravatar imagea_w_baker ( 2014-06-25 21:13:40 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2014-06-24 10:55:56 -0600

Seen: 1,989 times

Last updated: Jun 27 '14