Ask Your Question

Weird memory leak detected using opencv 2.4.8

asked 2014-02-10 18:18:37 -0600

DeepVoid gravatar image

updated 2014-02-14 02:34:08 -0600

I've created a completely empty new visual studio 2010 MFC project just trying to test the latest opencv 2.4.8, I added some header files to the stdafx.h as shown below:

image description

and added corresponding libs to the linker:

image description

then I added a menu non-popup button "Feat" to carry some testing codes of mine, only two lines:

image description

compiling and linking are both successful, but when I close the program, memory leak happens no matter I pressed button "Feat" or not:

image description

It's pretty weird to me, since even when I don't go into the OnFeat(), no cv::Mat are instantiated, there are still memory leaks detected. There will be no memory leaks only when I delete these two lines of codes in OnFeat(), I've also tried if I only instantiate a cv::Mat in OnFeat() not imread any images, there are still memory leaks detected no matter I go into the OnFeat() or not, but much less, as shown below:

image description

Has anybody encountered this kind of problem? Could it be the way I used opencv? am I doing something totally wrong with opencv? P. S. I've also tried 2.4.7, still has the same problem.

edit retag flag offensive close merge delete


By the way, I've tried both pre-compiled libs and libs compiled by myself, memory leaks detected in both cases.

DeepVoid gravatar imageDeepVoid ( 2014-02-11 16:33:04 -0600 )edit

Problem hasn't been solved yet, can anybody offer some helps on this?

DeepVoid gravatar imageDeepVoid ( 2014-02-12 17:57:08 -0600 )edit

Please if you want to bump, don't create a new answer, just go ahead and edit your question and add an update there. It gets bumped automatically then!

StevenPuttemans gravatar imageStevenPuttemans ( 2014-02-13 01:57:02 -0600 )edit

@StevenPuttemans Sorry about that, edit and add an update, got it, thank you very much.

DeepVoid gravatar imageDeepVoid ( 2014-02-13 02:03:25 -0600 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2014-02-15 08:57:31 -0600

May be you should try with release version of .lib files (opencv_core248.lib instead of opencv_core248d.lib and etc.) to see if the leak happens.

edit flag offensive delete link more


Thank you very much for your suggestion, but can memory leaks be detected under release mode? anyway, my .libs under release mode have always been those .libs without 'd' attached to the tail just like you suggested.

DeepVoid gravatar imageDeepVoid ( 2014-02-15 17:40:10 -0600 )edit

All right, normally you can not detect memory leak in Release build mode, but if you use release .lib files, your program will link to the .dll files built in release mode, this is different from .dll files built in debug mode (with 'd' attached in their tails) and if your program runs without any error, you may go with it and forget the error in the debug build mode. Additionally, the VLD can detect memory leak from your program even when it was built in release mode.

tuannhtn gravatar imagetuannhtn ( 2014-02-15 18:21:41 -0600 )edit

@tuannhtn my release built program can run without error, but I find that the memory keeps increasing as I keeps running codes multiple times, I'm worried maybe it will crash down at some point.

DeepVoid gravatar imageDeepVoid ( 2014-02-15 21:19:16 -0600 )edit

@DeepVoid: it is not easy to explain why your program increases memory without any knowledge of that. But I have some experience with memory problem of OpenCV on Windows: I have a program that iterates a function with different parameters, each time of running it allocates some very big Mat (the total memory can reach to ~1 Gb), after 9 hours of successful running, it stopped with an error informing that the program can not allocate the required memory. I could not explain exactly why because at that time, my free system memory is larger than 1Gb. I suppose the problem is due to the very big Mat: Windows does not have enough aligned memory to allocate such huge Mat object. Hope this help.

tuannhtn gravatar imagetuannhtn ( 2014-02-16 05:54:31 -0600 )edit

answered 2014-02-11 06:43:05 -0600

wl2776 gravatar image

Your includes look strange to me. Try to remove all C includes (starting with opencv/ ).

Also, try using Visual Leak Detector ( to get more sensible information about memory leaks.

edit flag offensive delete link more


@wl2776 thank you for your suggestion, I've blocked all C includes, and installed VLD, its header file vld.h is included in stdafx.h, now codes in stdafx.h looks like this:

include "opencv2/opencv.hpp"

include "opencv2/nonfree/nonfree.hpp"

include <vld.h>

but I still got memory leak detected by IDE, not by VLD, VLD reported no memory leak:

{135} normal block at 0x00874CA8, 61 bytes long. Data: < ( (M 0L > 00 00 00 00 28 00 00 00 28 4D 87 00 30 4C 87 00 {134} normal block at 0x00874C30, 56 bytes long. Data: < ( L > 00 00 00 00 28 00 00 00 A8 4C 87 00 00 00 00 00 Object dump complete. No memory leaks detected. Visual Leak Detector is now exiting.

P. S. I didn't press Feat button, i.e. OnFeat() hasn't been triggered.

DeepVoid gravatar imageDeepVoid ( 2014-02-11 07:55:10 -0600 )edit

Wrong usage of VLD. Include it into your cpp or header file,after stdafx or any other precompiled headers.

wl2776 gravatar imagewl2776 ( 2014-02-24 05:47:03 -0600 )edit

Question Tools


Asked: 2014-02-10 18:18:37 -0600

Seen: 3,477 times

Last updated: Feb 15 '14