IInspectable
IInspectable

Reputation: 51414

What's the resource management contract for IEnumString::Next?

I'm trying to implement an iterator over the IEnumString interface. I am having a hard time figuring out the precise contract of the IEnumString::Next() method.

The second parameter is documented as follows:

rgelt

An array of enumerated items.

The enumerator is responsible for allocating any memory, and the caller is responsible for freeing it.

The part that confuses me is how to properly manage memory. Apparently, the IEnumString implementation allocates memory that the caller is requested to free. That seems to imply that the OLESTR*'s received point to memory whose ownership has been transferred to the caller.

Is this how the documentation is to be interpreted? If so, which allocator should be used to free the memory?

Upvotes: 3

Views: 124

Answers (1)

catnip
catnip

Reputation: 25388

According to this Microsoft-authored sample on Github, the memory should be freed by calling CoTaskMemFree.

Start reading at line 91:

IFACEMETHODIMP CSampleSpellCheckProvider::InitializeWordlist(WORDLIST_TYPE wordlistType, _In_ IEnumString* words)
{
    unsigned int type = wordlistType;
    engine.ClearWordlist(type);

    HRESULT hr = S_OK;
    while (S_OK == hr)
    {
        LPOLESTR lpWord;
        hr = words->Next(1, &lpWord, nullptr);

        if (S_OK == hr)
        {
            hr = engine.AddWordToWordlist(type, lpWord);
            CoTaskMemFree(lpWord);
        }
    }

    return hr;
}

Upvotes: 3

Related Questions