Skip to content

Commit

Permalink
优化BSTR的使用,修复内存泄漏 (#13)
Browse files Browse the repository at this point in the history
* 优化BSTR的使用,修复内存泄漏

* 统一接口行为
  • Loading branch information
halx99 committed Jul 11, 2020
1 parent 480be6e commit 0352e4f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ ignored/*.*
*.suo
/.vs
/.xs
/build
/build*
4 changes: 2 additions & 2 deletions DmMain/inc/Widgets/DUIIE.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ namespace DM

/// @brief 获得当前页面
/// @param[in] pszURL 接收当前url的字符串缓冲区
/// @param[in] nMaxLen 字符串缓冲区的最大长度
/// @param[in] nMaxLen 字符串缓冲区的最大长度, 需要为L'\0'预留空间
/// @return HRESULT,失败为S_FALSE
HRESULT GetUrl(LPWSTR pszUrl, int nMaxLen);
CStringW GetUrl();
Expand Down Expand Up @@ -261,7 +261,7 @@ namespace DM
/// @param[in] strFun 指定要脚本执行的函数名称
/// @param[in] vecParams 给定要脚本执行的函数的参数列表
/// @param[out] strResult 返回脚本函数执行的结果
/// @param[in] nMaxLen 返回脚本函数执行的结果缓冲区的最大长度
/// @param[in] nMaxLen 返回脚本函数执行的结果缓冲区的最大长度, 需要为L'\0'预留空间
/// @return HRESULT,失败为E_FAIL
HRESULT ExecuteScriptFuntion(
LPCWSTR pszFun,
Expand Down
35 changes: 15 additions & 20 deletions DmMain/src/Widgets/DUIIE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,23 +757,20 @@ namespace DM
{
DMComPtr<IWebBrowser2> pWeb = Ptr();
if (!pWeb)
{
break;
}

BSTR _bsURL = NULL;
hr = pWeb->get_LocationURL(&_bsURL);
if (!SUCCEEDED(hr) || _bsURL == NULL)
break;
CStringA strUrlA =_com_util::ConvertBSTRToString(_bsURL) ;
CStringW strUrlW = DMA2W(strUrlA, CP_UTF8);
if (nMaxLen < strUrlW.GetLength())
{
break;
}
ZeroMemory(pszUrl, nMaxLen*sizeof(wchar_t));
memcpy(pszUrl, strUrlW, strUrlW.GetLength()*sizeof(wchar_t));

hr = S_OK;
UINT cch = ::SysStringLen(_bsURL);
if (nMaxLen > cch) {
memcpy(pszUrl, _bsURL, cch * sizeof(wchar_t));
pszUrl[cch] = L'\0';
hr = S_OK;
}
::SysFreeString(_bsURL);
} while (false);
return hr;
}
Expand All @@ -785,15 +782,14 @@ namespace DM
{
DMComPtr<IWebBrowser2> pWeb = Ptr();
if (!pWeb)
{
break;
}

BSTR _bsURL = NULL;
HRESULT hr = pWeb->get_LocationURL(&_bsURL);
if (!SUCCEEDED(hr) || _bsURL == NULL)
break;
CStringA strUrlA =_com_util::ConvertBSTRToString(_bsURL) ;
strUrl = DMA2W(strUrlA, CP_UTF8);
strUrl.Append(_bsURL, ::SysStringLen(_bsURL));
::SysFreeString(_bsURL);
} while (false);
return strUrl;
}
Expand Down Expand Up @@ -1131,12 +1127,11 @@ namespace DM

if (strResult != NULL && _varErr.vt == VT_BSTR)
{
CStringA strResultA =_com_util::ConvertBSTRToString(_varErr.bstrVal) ;
CStringW strResultW = DMA2W(strResultA, CP_UTF8);
if (nMaxLen > strResultW.GetLength())
int cch = ::SysStringLen(_varErr.bstrVal);
if (nMaxLen > cch)
{
ZeroMemory(strResult, nMaxLen*sizeof(wchar_t));
memcpy(strResult, strResultW, strResultW.GetLength()*sizeof(wchar_t));
memcpy(strResult, _varErr.bstrVal, cch * sizeof(wchar_t));
strResult[cch] = L'\0';
}
}
} while (false);
Expand Down

0 comments on commit 0352e4f

Please sign in to comment.