FIX: Invalid String Displayed by CToolTipCtrl (133257)
The information in this article applies to:
- The Microsoft Foundation Classes (MFC), when used with:
- Microsoft Visual C++, 32-bit Editions 2.1
- Microsoft Visual C++, 32-bit Editions 2.2
This article was previously published under Q133257 SYMPTOMS
A tooltip created with a CToolTipCtrl object displays an incorrect string
(or "garbage" string). This can happen under the following circumstances: - A temporary string buffer is passed to AddTool or SetToolInfo.
-or-
- The overloaded version of AddTool, which takes a resource ID as an
argument, is used.
CAUSE
The ToolTip common control stores string information about the tooltips it
is going to display. When you specify the string to use for a tooltip, you
pass a pointer to the buffer, which contains this string.
Unlike a list or combo box, a ToolTip control stores the pointer to the
string buffer instead of copying the string to its own local memory area.
Therefore, the string buffer passed in must be maintained as long as the
tooltip exists.
For example, the following code sequence will not work correctly:
int CMyWnd::OnCreate()
{
...
CString str("Helpful Info");
m_ToolTip.Create(this);
m_ToolTip.AddTool(this,str);
...
}
The CString object used in the call to AddTool is temporary. As soon as the
function exits, the string buffer is freed and the tooltip is pointing to
an invalid address. The overloaded version of CToolTip::AddTool, which
takes a resource ID as an argument, has this problem. This is easy to see
in the implementation of the function:
BOOL CToolTipCtrl::AddTool(CWnd* pWnd, UINT nIDText,
LPCRECT lpRectTool, UINT nIDTool)
{
ASSERT(nIDText != 0);
CString str;
VERIFY(str.LoadString(nIDText));
return AddTool(pWnd, str, lpRectTool, nIDTool);
}
RESOLUTION
You can either use hard-coded strings that will never be de-allocated or
you must store the string data for later use. One possible approach to
doing this is to create your own CToolTipCtrl-derived class, and store the
strings in it. You can add a OnAddTool handler for the TTM_ADDTOOL message
for this class. In the OnAddTool handler, allocate space for the string.
Then retrieve the permanent address of the stored string, and pass that in
the TOOLINFO structure to the default Window procedure.
An example of such a CToolTipCtrl-derived class is included in the "Sample
Code" section of this article.
STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed
at the beginning of this article. This bug was corrected in Microsoft
Visual C++, 32-bit edition, version 4.0.
Modification Type: | Major | Last Reviewed: | 10/17/2003 |
---|
Keywords: | kbBug kbcode kbfix kbToolTip KbUIDesign kbVC400fix KB133257 |
---|
|