Re: CTooltipCtrl with custom paint causing SYSTEM CRASH!!!!!
On Sep 30, 8:56 am, abhivg <abhi...@gmail.com> wrote:
Hi,
I have a CToolTipCtrl derived class which I am using to display text
as well as graphical data.
The tooltip class is handling the paint message in OnPaint() which
contains the text and drawing logic.
Following is a gist of the OnPaint():
///CODE START
CMyToolTip::OnPaint()
{
//CToolTipCtrl::OnPaint(); //if this is uncommented, =
below
drawing logic is not seen
if(someCondition)
{
CPaintDC dc(this);
CBrush br;
brCreateSolidBrush(RGB(0, 0, 255));
That's brush creation. If that failed, the rest is dubious. (IOW, you
better must check that)
CBrush* pOldBrush = dc.SelectObject(&br=
);
CFont font;
font.CreatePointFont(90, _T("MS Shell Dlg=
"));
That's font creation. If that failed, the rest is dubious.
CFont* pOldFont = dc.SelectObject(&font=
);
CRect rc;
GetWindowRect (&rc);
//modify rect coordinates here
MoveWindow (&rc); //resize th=
e tooltip window
//output some text
CRect txtRc(rc);
txtRc.right = l;
SetTextWidth(&dc, txtRc, m_str); =
//m_str
contains text to be written
dc.DrawText(m_str ,&txtRc, DT_LEFT);
//do some drawing
CRect someRect(somecoordinates,..);
dc.Rectangle(&someRect)
.
.
dc.SelectObject(pOldBrush);
::DeleteObject(dc.SelectObject(pOldFont))=
;
::DeleteObject(pOldBrush);
You must not do that. pOldBrush is not yours to delete.
Also, font GDI object is being deleted twice: once in ~CFont, once in
DeleteObject(dc.SelectObject(pOldFont));
You must not do that either.
SelectObject must only be used like so (not counting region objects):
oldObject = dc.SelectObject(&myObject);
if (!oldObject)
// Crap, error, can't continue!
workworkwork
dc.SelectObject(oldObject);
At any point from here, but not before, you can let myObject get
destroyed.
(The simplest way being creating it on the stack and letting it go out
of scope).
One should use RAII and scoping to force correct use, e.g.:
class CSelectObject /*probably noncopyable and without operator new*/
{
CSelectObject(CDC& cd, CGdiObject& o) : m_dc(dc), m_o(o)
{
m_pOld = m_dc.SelectOObject(&o);
if (NULL == m_pOld)
throw CGodDamnErrorException;
}
~CSelectObject()
{
VERIFY(m_dc.SelectObject(m_pOld);
}
CGdiObject* m_pOld;
};
and then:
OnPaint(...)
{
{
CSelectObject TempGDISelection(*pDC, myGdiObject1);
workworkwork
{
CSelectObject TempGDISelection(*pDC, myGdiObject2);
workworkwork
}
}
}
That said, none of that should cause BSOD. If it does, you should file
a bug report with Windows.
Goran.