IEnumString::Next 的资源管理合同是什么?
What's the resource management contract for IEnumString::Next?
我正在尝试通过 IEnumString
interface. I am having a hard time figuring out the precise contract of the IEnumString::Next()
方法实现迭代器。
第二个参数说明如下:
rgelt
An array of enumerated items.
The enumerator is responsible for allocating any memory, and the caller is responsible for freeing it.
让我困惑的部分是如何正确管理内存。显然,IEnumString
实现分配了请求调用者释放的内存。这似乎暗示 OLESTR*
接收到的内存指向其所有权已转移给调用者的内存。
这是文档的解释方式吗?如果是这样,应该使用哪个分配器来释放内存?
根据this Microsoft-authored sample on Github,应通过调用CoTaskMemFree
释放内存。
从第 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;
}
我正在尝试通过 IEnumString
interface. I am having a hard time figuring out the precise contract of the IEnumString::Next()
方法实现迭代器。
第二个参数说明如下:
rgelt
An array of enumerated items.
The enumerator is responsible for allocating any memory, and the caller is responsible for freeing it.
让我困惑的部分是如何正确管理内存。显然,IEnumString
实现分配了请求调用者释放的内存。这似乎暗示 OLESTR*
接收到的内存指向其所有权已转移给调用者的内存。
这是文档的解释方式吗?如果是这样,应该使用哪个分配器来释放内存?
根据this Microsoft-authored sample on Github,应通过调用CoTaskMemFree
释放内存。
从第 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;
}