Visual Studio 2015 + MFC Office Style + OpenCV 3.1 = Memory Leaks

asked 2016-02-03 07:25:06 -0500

tbecker gravatar image


i have problems using OpenCV 3.1 with Visual Studio 2015 (Prof.) and MFC.

First, i build the libraries dynamic:

I use CMake 3.4.1 and select the option "BUILD_SHARED_LIBS". The option "BUILD_WITH_STATIC_CRT" is not selected to build dynamically linked DLLs. At the test program (Simple MFC Office Style, no big extras), i use the opencv core, imgcodecs and imgproc to load a png file and run the canny filter over it. By closing the program, i got many memory leaks. There are the typical "MFC memory leaks" (but the solution to link MFC in OpenCV does not help at this point - the leaks still exists) and there are some leaks with a really high allocation number (the number between the curly brackets) and they seam to be inside of the dll.

Second, i build the libraries static:

For this example, i use the option "BUILD_WITH_STATIC_CRT" and "BUILD_SHARED_LIBS" is deselected. The result is, that most of the MFC typical leaks are gone (2 leaks exists) but the other leaks (with the high allocation number) are still there.

My test code in the 'OnInitialUpdate' method looks like (the loaded image is not displayed):

cv::Mat test;
test = cv::imread("image.png", CV_LOAD_IMAGE_UNCHANGED);

cv::Mat canny;

cv::Canny(test, canny, 50, 100);

So, now my question: Did i make some mistakes in using or building OpenCV? With the old version (2.4.11) and VS 2012, my way of building works good (with the addition of linking MFC in OpenCV).

Regards, Torben

edit retag flag offensive close merge delete


afaik, when using BUILD_SHARED_LIBS, you have to choose multi-threaded dynamic c-runtime, too.

if you choose BUILD_WITH_STATIC_CRT (because of MFC [baaa]) , you will need to build static opencv libs, and make sure, they use the same (static) c-runtime than your prog later.

berak gravatar imageberak ( 2016-02-03 07:41:32 -0500 )edit


I think the problem is well described in

so it is not as such a memory leak. But it makes it practically impossible for us to use OpenCV 3.1 as any memory leak we made have of our own will drown in all those reported (I have about 88 reports for just starting and stoppping the applicartion - some reported in OpenCV itself, some in the Intel TBB). I identified 2-3 of them in OpenCV and found those just to be static heap allocations of memory/objects).

Unfortunately, as seen from the problem will not be fixed.

So we are for now sticking to an older version of OpenCV.

Regards, also Torben.

PS: We are not using MFC.

ttn gravatar imagettn ( 2016-02-08 11:20:56 -0500 )edit