Memory leak with CvCapture

asked 2014-02-18 19:43:45 -0600

Gigatonn gravatar image

updated 2014-02-18 19:44:35 -0600


I was just testing a camera capture example and realized I was having memory leak problems. Trying to find out where the problem was, i just keep cutting the code until got this piece of code in c++:

int main(){

    CvCapture* input=cvCreateCameraCapture(1);
return 0;


Well, as you can see I am just allocating and releasing "input". Testing this on OpenCv 2.4.8 and 2.3.1 has the same effect. The code keep allocating memory over the time. In some sense, this code shoudn't increase the memory allocation over the time. Is this really a memory leaking or am I missing something?

ps: I am using visual c++ 2010 and codeblocks in windows 7.

edit retag flag offensive close merge delete


I don't think it is a bug of OpenCV here. Because you did not post all your code, it is not easy to say which causes that memory increasing. But usually, I saw code like that: int main() { CvCapture* input=cvCreateCameraCapture(0); while(<has more frame>){ <do your work here>;
} cvReleaseCapture(&input); return 0; } Hope this help.

tuannhtn gravatar imagetuannhtn ( 2014-02-19 07:52:09 -0600 )edit

Thank you for your time. I just executed this piece of code and let it run a while. The memory just keep growing at slow pace. The example I tested before seeing the leak problem was like the example you posted. In this case, I believe that cvReleaseCapture do not free the memory like I wanted to.

Gigatonn gravatar imageGigatonn ( 2014-02-19 13:47:59 -0600 )edit

I DO think this is a bug in OpenCV. I have been experiencing something similar with the python interface on a W95 system. The code opens and releases a VideoCapture device in an infinite loop to safe network bandwidth as I need an image only ever so often from the camera. I also see a steady increase in memory usage over time, which in my case leads to a computer crash every ~6 hours. Since I have switched to terminating and restarting the script before it can eat up all system resources no further computer crash has occured.

crash68 gravatar imagecrash68 ( 2014-02-27 01:23:02 -0600 )edit