double free or corruption when counting frames from video file

asked 2019-06-28 23:21:14 -0500

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 ){
            return 0;
        cv::Mat frame;
        //video_feed >> frame;
        int frame_count = 0;
        while( 1 ){
            video_feed >> frame;

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());
            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?

edit retag flag offensive close merge delete


What's your problem? You must check if frame is empty or not after imread or video>>frame

rcnt::44826 means that program reads 44827 frame 0,1,2, ... 44826

LBerger gravatar imageLBerger ( 2019-06-28 23:44:49 -0500 )edit

Oh, I tried calling empty on the loop right after video>>frame, but the problem comes whenever the last frame come the program crashes

a6502user gravatar imagea6502user ( 2019-06-29 07:15:09 -0500 )edit