Videocapture memory leak or feature?

hi contributor ,
here is my test code:


int main()
{
    cv::Mat x;
    std::string path{"a_video.file"};
    cv::VideoCapture * cap1 = new  cv::VideoCapture(path);
    cap1->read(x);// comment this it wont happen
    cv::VideoCapture * cap2 = new  cv::VideoCapture(path);
    cap2->read(x);// comment this it wont happen
    cv::VideoCapture * cap3 = new  cv::VideoCapture(path);
    cap3->read(x);// comment this it wont happen
    cv::VideoCapture * cap4 = new  cv::VideoCapture(path);
    cap4->read(x);// comment this it wont happen
    cv::VideoCapture * cap5 = new  cv::VideoCapture(path);
    cap5->read(x); // comment this it wont happen
    delete cap1;
    delete cap2;
    delete cap3;
    delete cap4;
    delete cap5;
//CV_VERSION 4.6.0 on ubuntu 16.04
    return 0;
}

this code will cause memory leak.the more new object the more memory consuming.
when comment all the read() functions , it doesnt.
is this a feature or bug ?
hope you guys check this problem.thanks!

please tell us, which backend (gstreamer ? ffmpeg?) you’re using.

and, how do you measure memleaks ?

hello. i dont know which backend i’m using . and this code auto find a backend to open my video.file.
measurement is the monitor on ubuntu.(i watched at it ! - -)
if you work on ubuntu , you can have a try ! just prepare any video file (best 4k) thank you !

export OPENCV_VIDEOIO_DEBUG=ON

and it will print out, what it does…

image
hi ,its ffmpeg backend .
hope this can help you to look into it .!

that’s not a memory leak. that’s just five VideoCapture instances with their required buffers created.

please learn about “memory leaks” and how to properly diagnose them.

and please learn about RAII. there are very few justified uses of new in C++ and this here is not justified.

thank you!
at first i think so.
but when i delete the videocapture, the “buffer” still exsit, so that makes me ask you for help.
and how can i remove the buffer when i dont need this videocapture any more?

what makes you say that?

i watch at it through the monitor on ubuntu16.04.when i place break point on each “read()” sentence, the memory goes high after every “read()”, and it wont goes down after delete that videocapture object. even reach out the return of main function. the memory wont goes down.

image

this app’s just do the code below and it wont release the memory when reach out the return .


int main()
{
    cv::Mat x;
    std::string path{"a_video.file"};
    cv::VideoCapture * cap1 = new  cv::VideoCapture(path);
    cap1->read(x);// comment this it wont happen
    cv::VideoCapture * cap2 = new  cv::VideoCapture(path);
    cap2->read(x);// comment this it wont happen
    cv::VideoCapture * cap3 = new  cv::VideoCapture(path);
    cap3->read(x);// comment this it wont happen
    cv::VideoCapture * cap4 = new  cv::VideoCapture(path);
    cap4->read(x);// comment this it wont happen
    cv::VideoCapture * cap5 = new  cv::VideoCapture(path);
    cap5->read(x); // comment this it wont happen
    delete cap1;
    delete cap2;
    delete cap3;
    delete cap4;
    delete cap5;
//CV_VERSION 4.6.0 on ubuntu 16.04
    return 0;
}

values in a System Monitor do not indicate memory leaks.

they indicate that the process still holds onto the memory, perhaps to reuse it later.

your “methodology” (exactly five instantiations) is invalid. run that stuff in an infinite loop, in the same process. if that causes continuously growing RAM usage, then you have a memory leak. it will not though.

basically… you won’t find a memory leak because smarter people would have found it already, and they didn’t, so there isn’t one. you would need to learn a lot more about memory leaks to understand what is a memory leak and what isn’t.

ok you are right .that is the OS holds my memory . i take it in a infinite loop and it wont goes high.

great thanks and sry for trouble you !