Memory leaks compiling C++ MFC App in debug mode

Hi guys,

i’m using OpenCV in a very simple C++ MFC application that i need to create with the following settings:

System information (version):

OpenCV => 4.5.4 and 4.3.0 Tested
Operating System / Platform => Windows 10 Pro
Compiler => : Visual studio 2019 pro

When i try to execute application in debug mode, it closes with a lot of (false?) memory leaks as shown below:

Detected memory leaks!
Dumping objects ->
{1455} normal block at 0x0000016A80306860, 16 bytes long.
 Data: <                > D0 0B 16 C7 FE 7F 00 00 02 00 00 00 CD CD CD CD 
{977} normal block at 0x0000016A802F4C10, 16 bytes long.
 Data: <                > 10 0C 16 C7 FE 7F 00 00 01 00 00 00 CD CD CD CD 
{845} normal block at 0x0000016A802C3CE0, 80 bytes long.
 Data: <        `       > 02 01 00 00 CD CD CD CD 60 BC 87 C7 FE 7F 00 00 
{769} normal block at 0x0000016A802DCE50, 8 bytes long.
 Data: <h       > 68 D4 0D C7 FE 7F 00 00 
{514} normal block at 0x0000016A802C01A0, 64 bytes long.
 Data: <                > 01 00 CD CD 00 00 00 00 00 00 00 00 00 00 00 00 
{469} normal block at 0x0000016A802C4770, 40 bytes long.
 Data: < B, j    B, j   > A0 42 2C 80 6A 01 00 00 A0 42 2C 80 6A 01 00 00 
{468} normal block at 0x0000016A802C4690, 40 bytes long.
 Data: < F, j    F, j   > 20 46 2C 80 6A 01 00 00 20 46 2C 80 6A 01 00 00 
{467} normal block at 0x0000016A802D3BE0, 16 bytes long.
 Data: <0 , j           > 30 06 2C 80 6A 01 00 00 00 00 00 00 00 00 00 00 
{466} normal block at 0x0000016A802C0620, 64 bytes long.
 Data: <  , j     , j   > 20 08 2C 80 6A 01 00 00 20 08 2C 80 6A 01 00 00 

Below a detail of project properties and the content of the OnInitDialog () method implemented:

Project Properties > Configuration Properties > C/C++>General >Additional Include Directories > D:\pencv_build\include\build\include
Project Properties > Configuration Properties > Linker > General > Additional Library Directories > D:\opencv_build\lib\Debug
Project Properties > Configuration Properties > Linker > Input > Additional Dependencies > opencv_core454d.lib;opencv_highgui454d.lib;opencv_imgcodecs454d.lib;opencv_imgproc454d.lib;opencv_videoio454d.lib
Project Properties > Configuration Properties > Linker > Input > Delay Loaded Dlls > opencv_core454d.dll;opencv_highgui454d.dll;opencv_imgcodecs454d.dll;opencv_imgproc454d.dll;opencv_videoio454d.dll
BOOL CMFCApplication1Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	Mat image1;

	image1 = imread("C:/Users/domen/Desktop/TestImg.jpg");

	// Check for failure
	if (!image1.empty())
	{
		String windowName = "ExampleImg"; //Name of the window

		namedWindow(windowName); // Create a window
		imshow(windowName, image1); // Show our image inside the created window.
		waitKey(0); // Wait for any keystroke in the window
		destroyWindow(windowName); //destroy the created window
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	return TRUE;  // return TRUE  unless you set the focus to a control
}

As sugested by other users, i’ve tried to apply the method that using delay loaded DLL, but it does not work.
How can I fix this problem?

Thanks in advance.

I also have several memory leaks just loading opencv 4 in debug mode. Could someone give me any suggestion on how to avoid them? They are very annoying because they let me distract from real application memory leaks.
thank you

any progress on this? i too find it very annoying. :frowning:

Strategic Leak Avoidance: Handling OpenCV Memory Leaks in MFC

I am posting this in 2026 for anyone struggling with this issue. After trying various methods, I’ve found that the most effective approach is Strategic Leak Avoidance.

The Root Cause

In MFC, these “leaks” often occur because OpenCV utilizes global memory pools or thread managers (like TBB/OpenMP) for performance optimization. These components are typically cleaned up only when the program is fully unloaded from memory (DLL Unload). Because MFC’s leak detector runs before this final cleanup, it incorrectly flags these internal OpenCV structures as active leaks.

The Solution

The strategy is to temporarily disable memory leak detection just before OpenCV’s DLL is first loaded and initialized. This prevents the initial internal allocations from being tracked.

Implementation Example

You can implement this in your InitInstance() or within the App constructor:

BOOL CMyProjectApp::InitInstance() {
    // [Start: Strategic Leak Avoidance]
    
    // 1. Save the current memory tracking state and turn off the Allocation flag
    int nOldState = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
    _CrtSetDbgFlag(nOldState & ~_CRTDBG_ALLOC_MEM_DF);

    // 2. Force OpenCV Initialization (call any simple function)
    // Internal memory allocated here won't be recorded as a leak!
    try {
        cv::Mat temp(10, 10, CV_8UC1); 
    } catch (...) {}

    // 3. Restore the memory tracking state to its original value
    _CrtSetDbgFlag(nOldState);

    // [End: Strategic Leak Avoidance]
    // From this point forward, any leaks in your own code will be correctly detected.
    
    // ... rest of your code
}

Essential Configuration

For this method to work, you must configure your project to delay the loading of the OpenCV binaries.

  • Go to: Project Properties > Linker > Input > Delay Loaded DLLs

  • Add the OpenCV DLL name (e.g., opencv_world4120d.dll).

By doing this, the DLL is loaded exactly when you call the dummy cv::Mat while tracking is off, rather than at application startup.