Ask Your Question

memory leak in OpenCV 3.4.0

asked 2017-12-28 02:34:20 -0500

updated 2018-11-19 06:13:26 -0500

berak gravatar image

I just noticed that some MFC application that use OpenCV is leaking memory. I reduced the functionality that use OpenCV , and surprisingly the memory still leak.

I create test application to simulate the issue and here is result

1-Create brand new MFC app, VS 2015, shared MFC. use Unicode. Test memory leak. No leak.

2-Link the app with opencv_world340d.lib. Test memory leak. No leak.

3-Just add #include <opencv2 opencv.hpp="">. to stdafx.h. Test memory leak. Leak exist, There are 635 leaked blocks and 114830 bytes in total.

4-reading an image from file using imread the total leaked block reduced to 323 block with 18069 bytes.

5-The dumb information is only reported by block, address and only size. No file names or source locations.

I suspect there is problem cv::String.

Any idea ?.

the issue appears initial on opencv-3.3.1-vc14

edit retag flag offensive close merge delete


"2-Link the app with opencv_world340d.lib" -- is that for a DEBUG project ?

also, please check with a non-MFC (a plain console, ascii) project.

berak gravatar imageberak ( 2017-12-28 02:39:05 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-11-19 05:16:15 -0500

DrLu gravatar image

updated 2018-11-29 05:26:58 -0500

This is not a real leak in the sense that you will run out of memory if you do some things over and over again. As far as I could gather, this seems to be by design see here and here (the second link describes a solution, but I am not good enough in c++ to work out the details) or google "static initialization order fiasco". You can reduce the number of things dumped if you force the program to unload the dll first, like so, e.g. in the ExitInstance() function of your app. Please note too, that I last used 3.10, I hope what I wrote is still up to date.

Edit: What most people seems to do is use MFC as static library to avoid the error.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-12-28 02:34:20 -0500

Seen: 527 times

Last updated: Nov 29