Memory leak when using IShellItem plus BindToHandler

Leo Purktz 0 Reputation points
2024-09-13T07:03:29.52+00:00

I write this code that iterates over all files on the desktop, its working correctly, however, the memory keeps increasing constantly.

I'm already using CComPtr to automatic manage the memory of these pointers, i'm trying to figure out whats leaking.

The unique place that with some items i get error is at pItem->BindToHandler

the error are:

E_NOINTERFACE No such interface supported. HRESULT_FROM_WIN32(ERROR_RESOURCE_TYPE_NOT_FOUND) : The specified resource type cannot be found in the image file.

When i comment pItem->BindToHandle the memory stops growing, what i'm missing?

#include <windows.h>
#include <iostream>
#include <vector>
#include <shlobj.h>
#include <atlcomcli.h>


void GetFileProperty(const std::wstring& filePath, std::vector<CComPtr<IShellItem>>& pItemList)
{
    HRESULT hr;
    if (pItemList.empty())
    {
        CComPtr<IShellItem> pItem = NULL;
        hr = SHCreateItemFromParsingName(filePath.c_str(), NULL, IID_PPV_ARGS(&pItem));
        if (FAILED(hr))
        {
            std::wcerr << L"SHCreateItemFromParsingName failed with HRESULT: " << hr << std::endl;
            return;
        }
        pItemList.push_back(pItem);
    }

    for (const CComPtr<IShellItem>& pItem : pItemList)
    {
        CComPtr<IPropertyStore> pPropertyStore;
        HRESULT hr = pItem->BindToHandler(NULL, BHID_PropertyStore, IID_PPV_ARGS(&pPropertyStore));
        if (FAILED(hr))
        {
            //std::wcerr << L"BindToHandler (BHID_PropertyStore) failed with HRESULT: " << hr << std::endl;
            continue;
        }
        // ...
    }
}

void enumDesktopFiles()
{
    CoInitialize(NULL);

    IShellFolder* pDesktop = NULL;
    HRESULT hr = SHGetDesktopFolder(&pDesktop);
    if (FAILED(hr))
    {
        std::wcerr << L"SHGetDesktopFolder failed with HRESULT: " << hr << std::endl;
        return;
    }

    IEnumIDList* pEnum = NULL;
    hr = pDesktop->EnumObjects(NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &pEnum);
    if (FAILED(hr))
    {
        std::wcerr << L"EnumObjects failed with HRESULT: " << hr << std::endl;
        pDesktop->Release();
        return;
    }

    std::vector<CComPtr<IShellItem>> pItemList;
    ITEMIDLIST* pidl = NULL;
    ULONG fetched;
    while (pEnum->Next(1, &pidl, &fetched) == S_OK)
    {
        CComPtr<IShellItem> pItem;
        hr = SHCreateItemFromIDList(pidl, IID_PPV_ARGS(&pItem));
        if (FAILED(hr))
        {
            std::wcerr << L"SHCreateItemFromIDList failed with HRESULT: " << hr << std::endl;
            CoTaskMemFree(pidl);
            continue;
        }
        pItemList.push_back(pItem);
        CoTaskMemFree(pidl);
    }

    pEnum->Release();
    pDesktop->Release();
    GetFileProperty(L"", pItemList);
    CoUninitialize();
}



int main()
{
    while (true)
    {
        enumDesktopFiles();
    }
}

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,693 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.