# Revision history [back]

### double free or corruption when counting frames from video file

I'm pretty much a total newbie when It comes to OpenCV, but I need to make an application that needs it for a project, so I wrote a simple C++ program that reads a video file and a counts the amounts of frames on it. However, as soon as the program gets to the last frame of the video, it returns the following error:

rcnt:   44824
rcnt:   44825
rcnt:   44826
double free or corruption (out)
fish: “bin/rcnt sample.mp4” terminated by signal SIGABRT (Abort)


When I check the amounts of frames with ffprobe using the following command,

ffprobe -v error -select_streams v:0 -show_entries stream=nb_frames -of default=nokey=1:noprint_wrappers=1 sample.mp4


I get 44827. After some trial and error, I believe this problem occurs whenever the cv::Mat::empty() function is called on the last frame, but I could also be wrong of course.

Either way, here is the code I wrote for this program:

...
int rcnt::init( void ){
if(should_abort)
return 0;
cv::Mat frame;
//video_feed >> frame;
int frame_count = 0;
while( 1 ){
video_feed >> frame;
if(frame.empty())
break;
rcnt::print_log(std::to_string(frame_count));
frame_count++;
}
}
...


The video_feed object was previously declared on at the begging of the namespace and initialize on the class constructor as:

video_feed = cv::VideoCapture(video_path.c_str());
if(!video_feed.isOpened()){
rcnt::print_log( "Error: Can't open video stream file." );
should_abort = true;


And then the program is called from the command line as the file to be read as the only argument. I know I could achieve the same goal by using video_feed.get(7) or even just ffprove, but the reason I'm doing it like this is because I'd like to get the overall brightness of each frame (I mean, I'd like to see which frames are brighter that others), and I thought this was a good way to start learning how to manipulate each frame individually.

So, any idea what I might be doing wrong in this case?