Ask Your Question
0

False memory leaks in MFC

asked 2017-01-05 07:39:11 -0600

Antares gravatar image

Is there someone who uses OpenCV in the MFC application? I don't believe that I am the only person on the world who is using this combination. I have really annoying problem with false memory leaks. It is impossible to detect any real memory leak because false memory leak report is huge. I tried everything what I found on the internet but nothing works.

I tried:

  • OpenCV 2.4, 3.0, 3.1. 3.2
  • DLL, static libs
  • Delay Loaded Dlls
  • Load MFC maualy before OpenCV Dlls

Nothing works. Still the same result. I'm out of ideas what to try. It is impossible to debug my application with these false memory leaks.

Please, Is there someone who is using Opencv and MFC without problem?

For example:

Code:

   #include <opencv2/core.hpp>
    ...    
    void OCVLeakTestDlg::OnBnClickedButton1()
    {
             cv::Mat m1(100,100,CV_8U);
    }
    ...

Result:

Detected memory leaks!
Dumping objects ->
{84} normal block at 0x003A58A0, 28 bytes long.
 Data: < X:             > D8 58 3A 00 FF FF FF FF 00 00 00 00 00 00 00 00 
{83} normal block at 0x003A5860, 4 bytes long.
 Data: < X: > A0 58 3A 00 
Object dump complete.
The program '[4204] OCVLeakTest.exe' has exited with code 0 (0x0).
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-01-13 14:16:40 -0600

This leak is real and caused by: sources\modules\core\src\system.cpp...

static Mutex* __initialization_mutex = NULL;
Mutex& getInitializationMutex()
{
    if (__initialization_mutex == NULL)
        __initialization_mutex = new Mutex();
    return *__initialization_mutex;
}

The problem is that there is a new and no matching delete. Since this pointer is a singleton, it only results in a leak when the application closes (so not REALLY a leak). However, if (like me) you are trying to find leaks in your own code reports such as this distract you from your task.

The fix for this leak that I am using is:

static Mutex* __initialization_mutex = NULL;
void deleteInitializationMutex()
{
    if (NULL != __initialization_mutex)
    {
        delete __initialization_mutex;
        __initialization_mutex = NULL;
    }
}
Mutex& getInitializationMutex()
{
    if (__initialization_mutex == NULL)
    {
        __initialization_mutex = new Mutex();
        atexit( deleteInitializationMutex );
    }
    return *__initialization_mutex;
}

If anybody wants to add this (or something like it) to the official code I would really appreciate it.

edit flag offensive delete link more

Comments

I have tried this, I am still having ML ... you did solved with this code ?

flaviu2 gravatar imageflaviu2 ( 2018-04-11 06:29:12 -0600 )edit

Thank you for the solution. Details of fixing:

  1. Add void deleteInitializationMutex() into system.cpp, see the code above.
  2. Rebuild all Opencv and move its libs into your Visual Studio.
  3. Open your MFC project.
  4. In the App class find (or create) the function ExitInstance().
  5. Add the following code there:

    namespace cv { extern void deleteInitializationMutex(); }

int CMFCApplication1App::ExitInstance() { cv::deleteInitializationMutex(); ......

}

6, Rebuild the project,

vicul gravatar imagevicul ( 2018-04-23 11:54:00 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-01-05 07:39:11 -0600

Seen: 2,796 times

Last updated: Jan 05 '17