The root problem is that you are returning the address of a local variable (sToolTip.m_pData)
from your OnToolTipText
handler. When control leaves the notification handler, sToolTip
goes out of scope and its destructor is run, leaving behind garbage.
To solve the issue you have 2 options:
- Copy the tooltip text to the
TOOLTIPTEXT::szText[]
array. - Increase the lifetime of the buffer you return so that it is available when the system needs it. The lifetime doesn't need to be any longer than the enclosing entity (the dialog in this case), so a class member of the dialog will do.
Unrelated to your question: When assigning a pointer to TOOLTIPTEXT::lpszText
member you should be using a const_cast
instead of calling CString::GetBuffer[SetLength]()
. The TOOLTIPTEXT
structure is used in both directions, to set and retrieve tooltip information. Consequently, the members cannot be declared const
, even if they are. It may look awkward but you would rather want to do the following:
pTTT->lpszText = const_cast<LPTSTR>(static_cast<LPCTSTR>(sToolTip));