Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my first submission, so I apologize up front if I'm doing it wrong.

I'm working in MSVC C++, in a Windows application with MFC support. I have spent the last 3 days online looking for suggestions, and the code below is what I have come up with. imgOut is a global CImage object created before this routine is called. In general the code works fine, but at just under 30000 cycles I get an error:

Unhandled exception at 0x516b70ee (mfc90d.dll) in Centroider.exe: 0xC0000005: Access violation reading location 0x0000000000000008.

Which the call stack points to: BOOL delDC = DeleteDC(mDC);

As you can see from the commented lines, I have tried a variety of approaches at cleaning up and nothing works.

I would greatly appreciate suggestions.
Thanks


C++
void DrawBitmap::DrawImg(){
  try{
    drawCnt++;

    CDC *screenDC = dlgPWnd->GetDC();
    CDC mDC;
    mDC.CreateCompatibleDC(screenDC);
    CBitmap b; 
    b.CreateCompatibleBitmap(screenDC, bmpXDest, bmpYDest);

    CBitmap *pob = mDC.SelectObject(&b);
    mDC.SetStretchBltMode(HALFTONE);
    imgOut.StretchBlt(mDC.m_hDC, 0, 0, bmpXDest, bmpYDest, 0, 0, imgOut.GetWidth(), imgOut.GetHeight(), SRCCOPY);
    mDC.SelectObject(pob);

    HWND pictHW = GetDlgItem( dlgHWnd, pictIdc );
    CWnd* pictCW = CWnd::FromHandle(pictHW);
    BOOL delObj = DeleteObject(((CStatic*)pictCW)->SetBitmap((HBITMAP)b.Detach()));

    pob->Detach();
    //mDC.DeleteDC();
    BOOL delDC = DeleteDC(mDC);
    //mDC.Detach();
    //mDC.ReleaseAttribDC();
    //mDC.ReleaseOutputDC();
    //DeleteObject(mDC);
    imgOut.Detach();
    dlgPWnd->ReleaseDC(screenDC);
  }catch(...){
    TRACE("DrawImg Exception\n");
  }
}
Posted
Updated 27-Jun-15 12:51pm
v2

1 solution

Some really convoluted code there but it could be in the following:
C++
// statements like this are almost impossible to debug or read.
    BOOL delObj = DeleteObject(((CStatic*)pictCW)->SetBitmap((HBITMAP)b.Detach()));
 
    pob->Detach();

You call Detach on the object b, and then immediately call it on pob which points to b, which is no longer valid.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900