# Videocapture memory leak or feature?

**URL:** <https://forum.opencv.org/t/videocapture-memory-leak-or-feature/11650>\
**Category:** C++\
**Tags:** programming\
**Created:** [January 17, 2023, 11:05am UTC](https://forum.opencv.org/t/videocapture-memory-leak-or-feature/11650 "2023-01-17T11:05:49Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![credox](https://avatars.discourse-cdn.com/v4/letter/c/8e7dd6/32.png) [@credox](https://forum.opencv.org/u/credox)\
**Post date:** [January 17, 2023, 11:05am UTC](https://forum.opencv.org/t/videocapture-memory-leak-or-feature/11650/1 "2023-01-17T11:05:49Z")

</div>

hi contributor ,  
here is my test code:

```auto

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!

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [January 17, 2023, 12:35pm UTC](https://forum.opencv.org/t/videocapture-memory-leak-or-feature/11650/2 "2023-01-17T12:35:17Z")

</div>

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

and, how do you measure memleaks ?

---

<div class="post-metadata">

**Author:** ![credox](https://avatars.discourse-cdn.com/v4/letter/c/8e7dd6/32.png) [@credox](https://forum.opencv.org/u/credox)\
**Post date:** [January 17, 2023, 1:12pm UTC](https://forum.opencv.org/t/videocapture-memory-leak-or-feature/11650/3 "2023-01-17T13:12:37Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [January 17, 2023, 1:37pm UTC](https://forum.opencv.org/t/videocapture-memory-leak-or-feature/11650/4 "2023-01-17T13:37:47Z")

</div>

> [@credox](#):
>
> i dont know which backend i’m using

```
export OPENCV_VIDEOIO_DEBUG=ON

```

and it will print out, what it does…

---

<div class="post-metadata">

**Author:** ![credox](https://avatars.discourse-cdn.com/v4/letter/c/8e7dd6/32.png) [@credox](https://forum.opencv.org/u/credox)\
**Post date:** [January 17, 2023, 2:39pm UTC](https://forum.opencv.org/t/videocapture-memory-leak-or-feature/11650/6 "2023-01-17T14:39:07Z")

</div>

![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/4/45d6ecb0654d22887e8c5839aa49c41573812a55.png)  
hi ,its ffmpeg backend .  
hope this can help you to look into it .!

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 17, 2023, 4:37pm UTC](https://forum.opencv.org/t/videocapture-memory-leak-or-feature/11650/7 "2023-01-17T16:37:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![credox](https://avatars.discourse-cdn.com/v4/letter/c/8e7dd6/32.png) [@credox](https://forum.opencv.org/u/credox)\
**Post date:** [January 17, 2023, 5:01pm UTC](https://forum.opencv.org/t/videocapture-memory-leak-or-feature/11650/8 "2023-01-17T17:01:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 17, 2023, 9:20pm UTC](https://forum.opencv.org/t/videocapture-memory-leak-or-feature/11650/9 "2023-01-17T21:20:36Z")

</div>

> [@credox](#):
>
> the “buffer” still exsit

what makes you say that?

---

<div class="post-metadata">

**Author:** ![credox](https://avatars.discourse-cdn.com/v4/letter/c/8e7dd6/32.png) [@credox](https://forum.opencv.org/u/credox)\
**Post date:** [January 18, 2023, 2:13am UTC](https://forum.opencv.org/t/videocapture-memory-leak-or-feature/11650/10 "2023-01-18T02:13:02Z")

</div>

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](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/d/d3c005625beca476ed52f622a3e8be4f5e6df95f.png)

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

```auto

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;
}

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 18, 2023, 3:09pm UTC](https://forum.opencv.org/t/videocapture-memory-leak-or-feature/11650/11 "2023-01-18T15:09:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![credox](https://avatars.discourse-cdn.com/v4/letter/c/8e7dd6/32.png) [@credox](https://forum.opencv.org/u/credox)\
**Post date:** [January 18, 2023, 3:57pm UTC](https://forum.opencv.org/t/videocapture-memory-leak-or-feature/11650/12 "2023-01-18T15:57:59Z")

</div>

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 !
