FIX: Setting Tooltip Text in OnToolHitTest Causes Assertion

Last reviewed: September 19, 1997
Article ID: Q156067
The information in this article applies to:
  • The Microsoft Foundation Classes (MFC) included with: - Microsoft Visual C++, 32-bit Edition, versions 4.0, 4.1, 4.2

SYMPTOMS

Attempting to set the text for a tooltip in the OnToolHitTest function of a CWnd by assigning a resource ID to the TOOLINFO struct's lpszText member causes the following assertion to occur:

   Debug assertion failed
   File: dbgheap.c
   Line: 1011

For example, setting the TOOLINFO struct as follows:

   pTI->lpszText= MAKEINTRESOURCE(SomeId);
   pTI->hinst=AfxGetInstanceHandle();

where pTI is a pointer to the TOOLINFO struct, will cause the problem. Setting the TOOLINFO struct members in this manner allows the programmer to specify a resource that the tooltip uses to get the text for the tooltip.

CAUSE

The CWnd::FilterToolTipMessage() function contains the following code:

   if (tiHit.lpszText != LPSTR_TEXTCALLBACK)
       free(tiHit.lpszText);

Because lpszText is not set to LPSTR_TEXTCALLBACK, the free function is called. However, a resource ID, rather than a string, was specified in the OnToolHitTest() function, and therefore the assertion occurs.

RESOLUTION

Instead of specifying a resource ID to load, load the resource and assign the text to the TOOLINFO struct's lpszText member as follows:

   pTI->lpszText = (char *)malloc(200, sizeof(char));
   if (::LoadString(AfxGetInstanceHandle(),pTI->uId, pTI->lpszText,200)==0)
   {
      ASSERT(FALSE);   //String resource doesn't exist
   }

MFC frees the allocated buffer after it has added the tool (that is, after it has sent the TTM_ADDTOOL message).

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This problem has been fixed in Visual C++ version 5.0.


Additional query words: tool tip
Keywords : MfcUI vcbuglist400 vcfixlist500
Technology : kbMfc
Version : WINDOWS NT:4.0,4.1,4.2
Platform : NT WINDOWS
Issue type : kbbug
Solution Type : kbfix


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 19, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.