@@ -50,8 +50,6 @@ LangBarButton::~LangBarButton(void) {
5050 textService_->Release ();
5151 if (menu_)
5252 ::DestroyMenu (menu_);
53- if (tooltip_) // allocated by wcsdup()
54- ::free (tooltip_);
5553}
5654
5755const wchar_t * LangBarButton::text () const {
@@ -89,23 +87,20 @@ void LangBarButton::setText(UINT stringId) {
8987
9088// public methods
9189const wchar_t * LangBarButton::tooltip () const {
92- return tooltip_;
90+ return tooltip_. c_str () ;
9391}
9492
9593void LangBarButton::setTooltip (const wchar_t * tooltip) {
96- if (tooltip_)
97- free (tooltip_);
98- tooltip_ = _wcsdup (tooltip);
94+ tooltip_ = tooltip;
9995 update (TF_LBI_TOOLTIP);
10096}
10197
10298void LangBarButton::setTooltip (UINT tooltipId) {
10399 const wchar_t * str;
104- int len = ::LoadStringW (textService_->imeModule ()->hInstance (), tooltipId, (LPTSTR)&str, 0 );
100+ // If this parameter is 0, then lpBuffer receives a read-only pointer to the resource itself.
101+ auto len = ::LoadStringW (textService_->imeModule ()->hInstance (), tooltipId, (LPTSTR)&str, 0 );
105102 if (str) {
106- tooltip_ = (wchar_t *)malloc ((len + 1 ) * sizeof (wchar_t ));
107- wcsncpy (tooltip_, str, len);
108- tooltip_[len] = 0 ;
103+ tooltip_ = std::wstring (str, len);
109104 update (TF_LBI_TOOLTIP);
110105 }
111106}
@@ -241,11 +236,8 @@ STDMETHODIMP LangBarButton::Show(BOOL fShow) {
241236}
242237
243238STDMETHODIMP LangBarButton::GetTooltipString (BSTR *pbstrToolTip) {
244- if (tooltip_) {
245- *pbstrToolTip = ::SysAllocString (tooltip_);
246- return *pbstrToolTip ? S_OK : E_FAIL;
247- }
248- return E_FAIL;
239+ *pbstrToolTip = ::SysAllocString (tooltip_.c_str ());
240+ return *pbstrToolTip ? S_OK : E_FAIL;
249241}
250242
251243// ITfLangBarItemButton
@@ -324,7 +316,7 @@ void LangBarButton::buildITfMenu(ITfMenu* menu, HMENU templ) {
324316 mi.fMask = MIIM_FTYPE|MIIM_ID|MIIM_STATE|MIIM_STRING|MIIM_SUBMENU;
325317 if (::GetMenuItemInfoW (templ, i, TRUE , &mi)) {
326318 UINT flags = 0 ;
327- wchar_t * text;
319+ wchar_t * text = nullptr ;
328320 ULONG textLen = 0 ;
329321 ITfMenu* subMenu = NULL ;
330322 ITfMenu** pSubMenu = NULL ;
0 commit comments