Ask Your Question
0

Weird memory leaks in MFC

asked 2016-11-04 05:17:33 -0600

Antares gravatar image

I have a problem with weird memory leaks. When I create simple MFC Dialog based application and add some Opencv code I get memory leaks warning even the OpenCV code is never executed. (OpenCV 3.1, VS11)

I have added only this:

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

And the result is:

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).

The code inside OnBnClickedButton1 function is never executed but it causing memory leaks. When I remove this code, it's ok without any memory leaks. I tried everything. Shared DLLs, Static libs, but still the same result. Maybe I forgot some settings, because I build OpenCV by myself.

edit retag flag offensive close merge delete

Comments

can you check your linker settings ? iirc if you start an empty MFC project, it links against different c-runtime, than the opencv libs were build against.

berak gravatar imageberak ( 2016-11-04 05:49:33 -0600 )edit
1

I checked it and both are Multi-threaded Debug (/MTd)

Antares gravatar imageAntares ( 2016-11-04 06:54:36 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-01-13 14:24:52 -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

Why is this not a problem with console applications?? If I make a wxWidgets project, I also get this memory error. However, if I use the same Opencv libs & dll's in a console app there are no reported leaks.

Chris gravatar imageChris ( 2019-03-11 09:41:32 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-11-04 05:17:33 -0600

Seen: 1,000 times

Last updated: Nov 04 '16