Search code examples
c++mfcgdi

MFC program crashes when window resized with error "A required resource was not found."


I'm developing an SDI MFC application where there are some custom buttons which are drawn in a custom way or have images loaded on top of them. When the running application is re-sized continuously for about 5 to 10 minutes, it crashes with the error "A required resource was not found."

I have checked the code thoroughly and all the GetDC() calls are followed by ReleaseDC() calls. Furthermore, I always save the old GDI object (for example, an oldBrush), whenever I do a DC.SelectObject(&newBrush) call and then restore the old pen with DC.SelectObject(&oldBrush).

Any hints for what else might be causing this error?

Edit: I used the program Deleaker to find the GDI leaks in the program and deleted those GDI objects which were causing the leak.

Edit: Here's the call stack for AfxThrowResourceException:

mfc110ud.dll!AfxThrowResourceException() Line 1353  C++
mfc110ud.dll!CWindowDC::CWindowDC(CWnd * pWnd) Line 1022    C++
mfc110ud.dll!CMFCToolBarImages::PrepareDrawImage(tagAFXDrawState & ds, CSize sizeImageDest, int bFadeInactive) Line 1219    C++
mfc110ud.dll!CMFCToolBarImages::DrawEx(CDC * pDC, CRect rect, int iImageIndex, CMFCToolBarImages::ImageAlignHorz horzAlign, CMFCToolBarImages::ImageAlignVert vertAlign, CRect rectSrc, unsigned char alphaSrc) Line 1729   C++
mfc110ud.dll!CMFCControlRenderer::FillInterior(CDC * pDC, CRect rect, CMFCToolBarImages::ImageAlignHorz horz, CMFCToolBarImages::ImageAlignVert vert, unsigned int index, unsigned char alphaSrc) Line 470  C++
mfc110ud.dll!CMFCControlRenderer::FillInterior(CDC * pDC, CRect rect, unsigned int index, unsigned char alphaSrc) Line 474  C++
mfc110ud.dll!CMFCControlRenderer::Draw(CDC * pDC, CRect rect, unsigned int index, unsigned char alphaSrc) Line 253  C++
mfc110ud.dll!CMFCVisualManagerOffice2007::DrawNcCaption(CDC * pDC, CRect rectCaption, unsigned long dwStyle, unsigned long dwStyleEx, const ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > & strTitle, const ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > & strDocument, HICON__ * hIcon, int bPrefix, int bActive, int bTextCenter, const CObList & lstSysButtons) Line 2097   C++
mfc110ud.dll!CMFCVisualManagerOffice2007::OnNcPaint(CWnd * pWnd, const CObList & lstSysButtons, CRect rectRedraw) Line 2343 C++
mfc110ud.dll!CFrameImpl::OnNcPaint() Line 1564  C++
mfc110ud.dll!CFrameWndEx::OnNcPaint() Line 1030 C++
mfc110ud.dll!CWnd::OnWndMsg(unsigned int message, unsigned int wParam, long lParam, long * pResult) Line 2459   C++
mfc110ud.dll!CWnd::WindowProc(unsigned int message, unsigned int wParam, long lParam) Line 2137 C++
mfc110ud.dll!AfxCallWndProc(CWnd * pWnd, HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Line 290   C++
mfc110ud.dll!AfxWndProc(HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Line 453    C++
mfc110ud.dll!AfxWndProcBase(HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Line 304    C++

Solution

  • I am sure you have a GDI or other Windows resource leak. The message text Comes from an internal exception in the MFC.

    What you see is the result of a call to AfxThrowResourceException. Set a break Point on this function in the Debugger and you can see what Operation Fails.

    As a result of this acion you know if you have a Memory or GDI leak, or may be other handle leak...