# OpenCV memory leak with wxwidgets

**URL:** <https://forum.opencv.org/t/opencv-memory-leak-with-wxwidgets/9915>\
**Category:** C++\
**Tags:** wxwidgets, programming, valgrind, leaks\
**Created:** [August 20, 2022, 9:03pm UTC](https://forum.opencv.org/t/opencv-memory-leak-with-wxwidgets/9915 "2022-08-20T21:03:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Glatzenhopper](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/glatzenhopper/32/4927_2.png) [@Glatzenhopper](https://forum.opencv.org/u/Glatzenhopper)\
**Post date:** [August 20, 2022, 9:03pm UTC](https://forum.opencv.org/t/opencv-memory-leak-with-wxwidgets/9915/1 "2022-08-20T21:03:41Z")

</div>

I found what I was looking for in the old forum, but there is no answer.

> **[OpenCV ?false? memory leak in VisualStudio - OpenCV Q&A Forum](https://answers.opencv.org/question/210166/opencv-false-memory-leak-in-visualstudio/)**
>
> Maybe this is not a true memory leak but it must be worrisome to some developers. I have seen similar posts to this around the web. My most recent experience is with wxWidgets and OpenCV 4.0.0 The single line below that declares a simple Mat makes...

In this link is a minimal example, which displays the error. Anyone know anything about this and how to fix 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:** [August 20, 2022, 9:32pm UTC](https://forum.opencv.org/t/opencv-memory-leak-with-wxwidgets/9915/2 "2022-08-20T21:32:55Z")

</div>

_that_ is a memory leak in user code that _purely_ relates to wxWidgets. that individual is `new`ing something and then throwing the pointer away.

it has **nothing at all** to do with OpenCV.

if _you_ have a question, ask it, but remember to present a MRE (minimal reproducible example, like on Stack Overflow). and you can be sure that a _mere_ “valgrind said something” will get no attention. valgrind complains whenever it’s in doubt. it’s not smart enough to figure out some programs are actually okay. and that is precisely the reason “valgrind said something” is unacceptable as a bug report, and incredibly tedious as a forum thread.

---

<div class="post-metadata">

**Author:** ![Glatzenhopper](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/glatzenhopper/32/4927_2.png) [@Glatzenhopper](https://forum.opencv.org/u/Glatzenhopper)\
**Post date:** [August 20, 2022, 11:09pm UTC](https://forum.opencv.org/t/opencv-memory-leak-with-wxwidgets/9915/3 "2022-08-20T23:09:21Z")

</div>

The problem is that if you comment out the Mat line in the example above, there is no memory leak because wxwidgets automatically clears the pointer to wxFrame. Only if the cv::Mat line is there does the memory leak occur.  
So what is wrong…?

---

<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:** [August 20, 2022, 11:40pm UTC](https://forum.opencv.org/t/opencv-memory-leak-with-wxwidgets/9915/4 "2022-08-20T23:40:16Z")

</div>

the Mat _will_ be properly cleaned up at the end of the scope. that much is sure. valgrind loves to complain but OpenCV’s Mat code has been checked manually.

feel free to browse “valgrind” issues here, especially the “invalid tracker” and “wontfix” ones: [Issues · opencv/opencv · GitHub](https://github.com/opencv/opencv/issues?q=valgrind)

you’ll note that Intel developers (they drive OpenCV development) may use valgrind, but they don’t blindly believe everything it says. see this comment:

> <https://github.com/opencv/opencv/issues/19469#issuecomment-774572226>
>
> \##### System information (version)
> \<!-- Example
> \- OpenCV =\> 4.1.1 
> \- Operatin…g System / Platform =\> Windows 64 Bit
> \- Compiler =\> Visual Studio 2015
> \--\>
> 
> \- OpenCV =\> 4.1.1
> \- Operating System / Platform =\> Windows 64 Bit
> \- Compiler =\> Visual Studio 2015  
> 
> \##### Detailed description
> 
> 
> 
> I use Qt Heob to check if there is any memory leak, while I using OpenCV 3.x. It doesn't cause memory leak. But when I change the OpenCV version to the latest one 4.5.1, I found that there cause memory leak. \_\_I also try OpenCV 4.5, 4.4, 4.3, 4.2, 4.1.2, 4.1.1, and I found that memory leak occurs while version \> 4.1.1.\_\_ I guess there some bugs while changing the version. 
> 
> Here is my testing code
> \`\`\`cpp
> cv::Mat mat;
> mat.create(3000, 1000, CV\_8UC3);
> \_temp = mat.clone();
> \`\`\`
> 
> Memory leak occurs while using clone().
> 
> !\[image\](https://user-images.githubusercontent.com/10307121/107113861-abb6c180-689c-11eb-814e-17b2c950cfd8.png)
> 
> 
> \##### Steps to reproduce
> 
> \<!-- to add code example fence it with triple backticks and optional file extension
> \`\`\`.cpp
> // C++ code example
> \`\`\`
> or attach as .txt or .zip file
> \--\>
> 
> \##### Issue submission checklist
> 
> - \[x\] I report the issue, it's not a question
> \<!--
> OpenCV team works with forum.opencv.org, Stack Overflow and other communities
> to discuss problems. Tickets with question without real issue statement will be
> closed.
> --\>
> - \[x\] I checked the problem with documentation, FAQ, open issues,
> forum.opencv.org, Stack Overflow, etc and have not found solution
> \<!--
> Places to check:
> \* OpenCV documentation: https://docs.opencv.org
> \* FAQ page: https://github.com/opencv/opencv/wiki/FAQ
> \* OpenCV forum: https://forum.opencv.org
> \* OpenCV issue tracker: https://github.com/opencv/opencv/issues?q=is%3Aissue
> \* Stack Overflow branch: https://stackoverflow.com/questions/tagged/opencv
> --\>
> - \[x\] I updated to latest OpenCV version and the issue is still there
> \<!--
> master branch for OpenCV 4.x and 3.4 branch for OpenCV 3.x releases.
> OpenCV team supports only latest release for each branch.
> The ticket is closed, if the problem is not reproduced with modern version.
> --\>
> - \[x\] There is reproducer code and related data files: videos, images, onnx, etc
> \<!--
> The best reproducer -- test case for OpenCV that we can add to the library.
> Recommendations for media files and binary files:
> \* Try to reproduce the issue with images and videos in opencv\_extra repository
> to reduce attachment size
> \* Use PNG for images, if you report some CV related bug, but not image reader
> issue
> \* Attach the image as archive to the ticket, if you report some reader issue.
> Image hosting services compress images and it breaks the repro code.
> \* Provide ONNX file for some public model or ONNX file with with random weights,
> if you report ONNX parsing or handling issue. Architecture details diagram
> from netron tool can be very useful too. See https://lutzroeder.github.io/netron/
> --\>
