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
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();
BOOL delDC = DeleteDC(mDC);
imgOut.Detach();
dlgPWnd->ReleaseDC(screenDC);
}catch(...){
TRACE("DrawImg Exception\n");
}
}