When doing multi-threading errors, in fact, the problem is somewhere else. Where the assertion error occurs below
Original error code:
Draw Green Ball
if (M_isgreenball)
{
CDC *PDC = M_greenrect. GetDC ();
CBrush *poldbrush;
CBrush Newbrush (RGB (0,255,0));
Poldbrush=pdc->selectobject (&newbrush);
Pdc->ellipse (Greenball.pos,0,greenball.pos+greenball.rect.height (), Greenball.rect.Height ());
Pdc->selectobject (Poldbrush);
Pdc->deletedc ();
}
Where to assert the error:
void Chandlemap::removehandle (HANDLE h)
{
Make sure the handle entry is consistent before deleting
cobject* ptemp = lookuptemporary (h);
if (ptemp! = NULL)
{
Temporary objects must have correct handle values
handle* ph = (handle*) ((byte*) ptemp + m_noffset); After CObject
ASSERT (ph[0] = = H | | ph[0] = = NULL);
if (m_nhandles = = 2)
ASSERT (ph[1] = = h); Error location here
}
Ptemp = Lookuppermanent (h);
if (ptemp! = NULL)
{
handle* ph = (handle*) ((byte*) ptemp + m_noffset); After CObject
ASSERT (ph[0] = = h);
Permanent object may has secondary handles that is different
}
Remove only from permanent map--temporary objects is removed
At idle in Chandlemap::D eletetemp, always!
M_permanentmap.removekey ((LPVOID) h);
}
#endif
Later find the book found that the handle was released wrong, many thanks to Xinxin Sun teacher.
The DC that is obtained with GETDC () must call ReleaseDC ()
A DC created with CREATEDC () must call DeleteDC ()
Difference:
ReleaseDC () only releases a reference to the DC
DeleteDC () is the deletion of the DC object, using GetDC (), the obvious that the DC is not created by ourselves, using DeleteDC () will be someone else (usually the MFC framework) created by the DC killed, so that when others want to use the DC created by themselves, it will produce an exception.
ReleaseDC () Just solves this problem by releasing a reference that tells the creator DC that I'm done.
The following is the modified code:
Draw Green Ball
if (M_isgreenball)
{
CDC *PDC = M_greenrect. GetDC ();
CBrush *poldbrush;
CBrush Newbrush (RGB (0,255,0));
Poldbrush=pdc->selectobject (&newbrush);
Pdc->ellipse (Greenball.pos,0,greenball.pos+greenball.rect.height (), Greenball.rect.Height ());
Pdc->selectobject (Poldbrush);
ReleaseDC (PDC);
}
Chandlemap::removehandle (HANDLE h) Error in assertion