Adding ToolInfo into a Tooltip cause the user object leak  
Author Message
sonkanit





PostPosted: Visual C++ General, Adding ToolInfo into a Tooltip cause the user object leak Top

I wrote a small simple WTL program which create a new window with 2 buttons. These buttons pass their message to the new window using CContainedWindow. When I try to add the ToolInfo for each button tooltip, there is a user object created. 1 user object per CToolTipCtrl::AddTool or ::SendMessage(TTM_ADDTOOL) call. Those created user objects are not destroyed properly when the window is destroy.

Actually I wrote this simple program to imitate my bigger program that I could not put the sourcecode here. The following code is the snapshot from my simple leaky one. Note that CToolBox is the window that contain two buttons.

LRESULT CToolBox::OnToolBoxCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)

{

//Create A Button

RECT rc1,rc2;

rc1.left = 0; rc1.right = 100; rc1.top = 0; rc1.bottom =30;

rc2.left = 0; rc2.right = 100; rc2.top = 35; rc2.bottom =65;

m_oToolTip.Create(m_hWnd,rc1,0,WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,WS_EX_TOPMOST);

m_oToolTip.SetWindowPos(HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);

/*If I use this code block instead of the following block, there is no leak in WindowsXP theme but in the Window classic theme !Weird

m_oButton1.Create(m_hWnd, rc1, "Button1", WS_CHILD|WS_VISIBLE);

m_oButton2.Create(m_hWnd, rc2, "Button2", WS_CHILD|WS_VISIBLE);

m_wndButton1.SubclassWindow(m_oButton1.m_hWnd);

m_wndButton2.SubclassWindow(m_oButton2.m_hWnd);

CToolInfo ti1(TTF_IDISHWND | TTF_SUBCLASS, m_hWnd, (UINT)m_oButton1.m_hWnd, 0, "B1");

m_oToolTip.AddTool(&ti1);

CToolInfo ti2(TTF_IDISHWND | TTF_SUBCLASS, m_hWnd, (UINT)m_oButton2.m_hWnd, 0, "B2");

m_oToolTip.AddTool(&ti2);

*/

m_oButton1.Create(m_hWnd, rc1, "Button1", WS_CHILD|WS_VISIBLE);

m_oButton2.Create(m_hWnd, rc2, "Button2", WS_CHILD|WS_VISIBLE);

CreateToolTipButton(m_oButton1.m_hWnd, &m_wndButton1, rc1, "Button1");

CreateToolTipButton(m_oButton2.m_hWnd, &m_wndButton2, rc2, "Button2");

return 0;

}

void CToolBox::CreateToolTipButton(CButton* pWnd, CContainedWindow* pCnt,RECT& rc,LPSTR szText)

{

CButton* pmWnd = (CButton*)pWnd;

pWnd->Create(m_hWnd, rc, szText, WS_CHILD|WS_VISIBLE);

pCnt->SubclassWindow(pWnd->m_hWnd);

SetToolTipText(pWnd, szText);

}

The buttons messages that are sent to CToolBox are WM_MOUSELEAVE and WM_LBUTTONDOWN.

As in my code comment, my solution only works in WindowsXP theme but not Windows Classic theme. Also both don't work in my real bigger application and there is no leak if I don't subclass those buttons. So my questions are how to manage the tooltip properly with subclassed buttons Are there any specific methods Why does the theme has the effect to my code The more buttons with tooltips I added the more leak I got.

Thank you forward.



Visual C++13