tooltip problem

I add tooltips to a child window like:
ti.cbSize = sizeof(TOOLINFO);
ti.uFlags = TTF_TRANSPARENT | TTF_CENTERTIP | TTF_SUBCLASS;
ti.hwnd = hParent; // is valid
ti.uId = iToolTipId;
ti.rect = rectToolTip;
ti.lpszText = pString; // allocated and saved
SendMessage(hToolTip,TTM_ADDTOOL,0,(LPARAM)&ti);

Before destroying the window (or before changing the tooltips) I do the following:
while (SendMessage(hToolTip,TTM_ENUMTOOLS,0,(LPARAM)&ti)) {
   SendMessage(hToolTip,TTM_DELTOOL,0,(LPARAM)&ti);
} // while look for tooltips to get rid of

There is no problem when I remove the tooltips in my display routine which updates the text of the tooltips.  But I get an access violation on the first SendMessage() when closing things down.  It is called when it's parent gets a WM_LBUTTONUP message on it's Finished button.  3 questions:
1)  Do I need to do this when exitting? (Are there resources, besides the text, that need to be freed)
2)  Does the text for ti.lpszText need to valid for the lifetime of the tooltip, or can it be a local string.
3)  If 'Yes' to question 1, what might be causing the access violation?
Thank You.
LVL 1
marvinmAsked:
Who is Participating?
 
ScottyDawgCommented:
Ah, just noticed what's going on (I think).

When you send your message TTM_ENUMTOOLS, the tooltip fills in all the details of the tool as well as the test of the tool.

So, for the pszText, if you leave it null, it wont bother filling in the text - but if its not NULL it assumes its a pointer to a buffer big enough to contain the text. In this case, you haven't allocated any space for it and its overwrites memory elsewhere causing the problem :)






0
 
ScottyDawgCommented:
1) Have you called the Default() implementation of WM_LBUTTONUP first?

2) Sadly yes, the tooltip text must be static or allocated on the heap.

0
 
marvinmAuthor Commented:
I try to delete the tooltips at the end of the WM_LBUTTONUP case, right before "return 0;"  If I set ti.lpszText = NULL, I do not get the error.  I noticed that at the time of the error, all of the TOOLINFO is set, but the lpszText contains garbage.  This is if I set to an allocated pointer that has not yet been freed, or to static text such as "".
0
 
marvinmAuthor Commented:
With a quick test, it does seem to work ok if i allocate the lpszText pointer before sending the TTM_ENUMTOOLS message.  I am wondering why it does not just set this to the pointer that was set in the TTM_ADDTOOL message?
I will test some more and if all goes well I will grade.
Thank You, mm
0
 
ScottyDawgCommented:
Marvin, in my experience most controls want you to allocate storage in some way when interrogating them for strings. Albeit, most actually provide an int somewhere in the structure for you to specify how big the string is - TOOLINFO seems to be the exception.

I guess the reason for this is that if they pass you backa pointer to the data you could do what you liked to the contents, perhaps they're just being careful. And of course, the string may not be stored in the way you expected i.e. UNICODE or DBCS or....

but its still a real pain to work with :)

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.