Chandlemap::removehandle (HANDLE h) Error in assertion

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.