Why do repeated calls to FileOpenDialog cause a memory leak?

124 Views Asked by At

I tried to implement a simple FileOpenDialog into my program using COM. But i noticed that when multiple successfull calls were made to select files using FileOpenDialog, a Memory Leak occured. I decided to copy this exact example from msdn, but if i add a loop to this example, i.e. like this:

#include <windows.h>
#include <shobjidl.h> 

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
    HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |
        COINIT_DISABLE_OLE1DDE);
    while(true)
    {
        IFileOpenDialog* pFileOpen;

        // Create the FileOpenDialog object.
        hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
            IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));

        if (SUCCEEDED(hr))
        {
            // Show the Open dialog box.
            hr = pFileOpen->Show(NULL);
            // Get the file name from the dialog box.
            if (SUCCEEDED(hr))
            {
                IShellItem* pItem;
                hr = pFileOpen->GetResult(&pItem);
                if (SUCCEEDED(hr))
                {
                    PWSTR pszFilePath;
                    hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);

                    // Display the file name to the user.
                    if (SUCCEEDED(hr))
                    {
                        MessageBox(NULL, pszFilePath, L"File Path", MB_OK);
                        CoTaskMemFree(pszFilePath);
                    }
                    pItem->Release();
                }
            }
            pFileOpen->Release();
        }
    }
    CoUninitialize();
    return 0;
}

The Process Memory still increases every time i select a file. I do not understand why repeated calls to this code would cause a Memory Leak.

![enter image description here

1

There are 1 best solutions below

0
Artem Razin On BEST ANSWER

Profiled code by Deleaker, compared snapshots and indeed got numerous leaks (see picture below).

At the same time, the code itself is clean (though it's always a good idea to avoid explicit Release() calls: smart pointers are our good friends).

As a workaround I would use a single IFileOpenDialog.

Leaks