Reputation: 21
I have a problem to open many video files (e.g., 200) in a loop by using the OpenCV class VideoCapture. Below you can find my code.
More specifically, my program succeeds to open a certain number of videos (usually 171-173) but then it fails to open the others. I even tried to open always the same file (like in the example below) but the behaviour is the same.
In my opinion it should not be a memory leak problem (actually there is memory leak, but consumes only about 50MB in total). I think it is related to the fact that, when each video is opened, several threads are also opened and never closed, so they accumulate. But I don't know if this is the real reason or, if it is, how to solve it.
I'm using Visual Studio to compile, and Windows 7 OS.
Please let me know if you have any clue and/or solution.
string video_filename = "MyVideo.mp4";
for(int j=0; j<200; j++)
{
VideoCapture video(video_filename);
if(!video.isOpened())
{
cout << "Video #" << j << " could not be opened" << endl;
}
video.release(); // I've tried also to comment this out
}
I think you can easily try to reproduce this problem, as the code is very simple.
Upvotes: 2
Views: 7005
Reputation: 93410
I used OpenCV 2.3.0 on Mac OS X and had no problems running your code.
You might want to upgrade your version to 2.3.1 and try again. If the problem persists, it might an issue specific to the Windows implementation or even maybe just specific to Windows 7.
Another wild guess is to implement the program above using the C interface of OpenCV instead of the C++ interface you are using right now. I've had problems in the past (not related to video) that were fixed using this trick. I don't recommend mixing the interfaces, so if you are going to do something with the C interface, don't use the C++ interface of OpenCV in your program:
for (int j=0; j<200; j++)
{
CvCapture* capture = cvCaptureFromAVI("MyVideo.mp4");
if (!capture)
{
cout << "Video #" << j << " could not be opened" << endl;
// Prevent calling cvReleaseCapture() on a capture that didn't succeeded
continue;
}
cvReleaseCapture(&capture);
}
I don't remember if it's cvCaptureFromAVI()
or cvCreateFileCapture()
. Please verify!
Upvotes: 1