返回 BSTR 时出现 MemoryLeak 或 DeLeaker 误报

MemoryLeak or false positive of DeLeaker when returning BSTR

我有 COM 函数 returns 一个 BSTR。它是这样使用的:

接口:

interface ITexts : IDispatch
{
    [id(5)] HRESULT GetText([in] long Number, [in] long LangID, [out,retval] BSTR* pText);
};

实施

STDMETHODIMP CTexts::GetText(long Number, long LangID, BSTR *pText)
{
    CString sDummyValue = _T("gettext"); //just for testing
    sDummyValue.SetSysString(pText);
    return S_OK;
}

和客户

CString RITexts::GetText(long Number, long LangID)
{
    CString result;
    static BYTE parms[] = VTS_I4 VTS_I4;
    InvokeHelper(0x5, DISPATCH_METHOD, VT_BSTR, (void*)&result, parms, Number, LangID);
    return result;
}

当运行 as debug-build Visual Studio 没有报错但DeLeaker(内存泄漏监控工具)认为存在内存泄漏时:

OLEAUT32.dll!SysReAllocStringLen 76543120
mfc140ud.dll!ATL::ChTraitsCRT<wchar_t>::ReAllocSysString Line 815 + 0xe bytes 7a2d0012
mfc140ud.dll!ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >::SetSysString Line 2288 + 0x16 bytes 7a2d0a39
(-> ComClass) LangSupp.dll!CTexts::GetText Line 343 + 0x9 bytes 7b1698b2
OLEAUT32.dll!DispCallFunc + 0x16d bytes 7655840d
OLEAUT32.dll!CTypeInfo2::Invoke + 0x2e6 bytes 7653aca6
LangSupp.dll!ATL::CComTypeInfoHolder::Invoke Line 4204 + 0x31 bytes 7b15c056
LangSupp.dll!ATL::IDispatchImpl<ITexts,&IID_ITexts,&LIBID_LANGSUPPLib,1,0,ATL::CComTypeInfoHolder>::Invoke Line 5163 + 0x2a bytes 7b15bfa2
mfc140ud.dll!COleDispatchDriver::InvokeHelperV Line 399 + 0x7 bytes 7a1e6d6b
mfc140ud.dll!COleDispatchDriver::InvokeHelper Line 554 + 0x1d bytes 7a1e6597
(-> Client) Managers.dll!RITexts::GetText Line 104 + 0x1b bytes 7ac2ce3a 

这是 DeLeaker 的误报,还是我遗漏了一些有关释放字符串的信息?

调用InvokeHelper不正确。

result 的类型应该是 BSTR 或更好地使用像 _bstr_t or CComBSTR 这样的包装器来确保为 BSTR 分配的内存将被释放.

服务器正确,客户端错误。您想要使用变体类型从自动化调用中获取结果。尝试:

CString RITexts::GetText(long Number, long LangID)
{
    static BYTE parms[] = VTS_I4 VTS_I4;
    _variant_t vResult;
    InvokeHelper(0x5, DISPATCH_METHOD, VT_VARIANT, (void*)&vResult, parms, Number, LangID);
    CString result((wchar_t*) vResult.bstrVal); // should have error checking of your result and type
    return result;
}