Skip to content

Commit 0352e4f

Browse files
authored
优化BSTR的使用,修复内存泄漏 (#13)
* 优化BSTR的使用,修复内存泄漏 * 统一接口行为
1 parent 480be6e commit 0352e4f

File tree

3 files changed

+18
-23
lines changed

3 files changed

+18
-23
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ ignored/*.*
2020
*.suo
2121
/.vs
2222
/.xs
23-
/build
23+
/build*

DmMain/inc/Widgets/DUIIE.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ namespace DM
198198

199199
/// @brief 获得当前页面
200200
/// @param[in] pszURL 接收当前url的字符串缓冲区
201-
/// @param[in] nMaxLen 字符串缓冲区的最大长度
201+
/// @param[in] nMaxLen 字符串缓冲区的最大长度, 需要为L'\0'预留空间
202202
/// @return HRESULT,失败为S_FALSE
203203
HRESULT GetUrl(LPWSTR pszUrl, int nMaxLen);
204204
CStringW GetUrl();
@@ -261,7 +261,7 @@ namespace DM
261261
/// @param[in] strFun 指定要脚本执行的函数名称
262262
/// @param[in] vecParams 给定要脚本执行的函数的参数列表
263263
/// @param[out] strResult 返回脚本函数执行的结果
264-
/// @param[in] nMaxLen 返回脚本函数执行的结果缓冲区的最大长度
264+
/// @param[in] nMaxLen 返回脚本函数执行的结果缓冲区的最大长度, 需要为L'\0'预留空间
265265
/// @return HRESULT,失败为E_FAIL
266266
HRESULT ExecuteScriptFuntion(
267267
LPCWSTR pszFun,

DmMain/src/Widgets/DUIIE.cpp

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -757,23 +757,20 @@ namespace DM
757757
{
758758
DMComPtr<IWebBrowser2> pWeb = Ptr();
759759
if (!pWeb)
760-
{
761760
break;
762-
}
761+
763762
BSTR _bsURL = NULL;
764763
hr = pWeb->get_LocationURL(&_bsURL);
765764
if (!SUCCEEDED(hr) || _bsURL == NULL)
766765
break;
767-
CStringA strUrlA =_com_util::ConvertBSTRToString(_bsURL) ;
768-
CStringW strUrlW = DMA2W(strUrlA, CP_UTF8);
769-
if (nMaxLen < strUrlW.GetLength())
770-
{
771-
break;
772-
}
773-
ZeroMemory(pszUrl, nMaxLen*sizeof(wchar_t));
774-
memcpy(pszUrl, strUrlW, strUrlW.GetLength()*sizeof(wchar_t));
775766

776-
hr = S_OK;
767+
UINT cch = ::SysStringLen(_bsURL);
768+
if (nMaxLen > cch) {
769+
memcpy(pszUrl, _bsURL, cch * sizeof(wchar_t));
770+
pszUrl[cch] = L'\0';
771+
hr = S_OK;
772+
}
773+
::SysFreeString(_bsURL);
777774
} while (false);
778775
return hr;
779776
}
@@ -785,15 +782,14 @@ namespace DM
785782
{
786783
DMComPtr<IWebBrowser2> pWeb = Ptr();
787784
if (!pWeb)
788-
{
789785
break;
790-
}
786+
791787
BSTR _bsURL = NULL;
792788
HRESULT hr = pWeb->get_LocationURL(&_bsURL);
793789
if (!SUCCEEDED(hr) || _bsURL == NULL)
794790
break;
795-
CStringA strUrlA =_com_util::ConvertBSTRToString(_bsURL) ;
796-
strUrl = DMA2W(strUrlA, CP_UTF8);
791+
strUrl.Append(_bsURL, ::SysStringLen(_bsURL));
792+
::SysFreeString(_bsURL);
797793
} while (false);
798794
return strUrl;
799795
}
@@ -1131,12 +1127,11 @@ namespace DM
11311127

11321128
if (strResult != NULL && _varErr.vt == VT_BSTR)
11331129
{
1134-
CStringA strResultA =_com_util::ConvertBSTRToString(_varErr.bstrVal) ;
1135-
CStringW strResultW = DMA2W(strResultA, CP_UTF8);
1136-
if (nMaxLen > strResultW.GetLength())
1130+
int cch = ::SysStringLen(_varErr.bstrVal);
1131+
if (nMaxLen > cch)
11371132
{
1138-
ZeroMemory(strResult, nMaxLen*sizeof(wchar_t));
1139-
memcpy(strResult, strResultW, strResultW.GetLength()*sizeof(wchar_t));
1133+
memcpy(strResult, _varErr.bstrVal, cch * sizeof(wchar_t));
1134+
strResult[cch] = L'\0';
11401135
}
11411136
}
11421137
} while (false);

0 commit comments

Comments
 (0)