1

next is my code (debug mode run error, release no problem):

"CControlPanel" class derived from CFormView

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
    m_wndSplitterFrame.CreateStatic(this, 2, 1);
    m_wndSplitterFrame.CreateView(0, 0, RUNTIME_CLASS(CControlPanel), CSize(0, 180), pContext);  //assert error line
    m_wndSplitterSchema.CreateStatic(&m_wndSplitterFrame, 1, 2, WS_CHILD | WS_VISIBLE, m_wndSplitterFrame.IdFromRowCol(1, 0));
    m_wndSplitterSchema.CreateView(0, 0, RUNTIME_CLASS(CTabView), CSize(250, 200), pContext);
    m_wndSplitterData.CreateStatic(&m_wndSplitterSchema, 1, 2, WS_CHILD | WS_VISIBLE, m_wndSplitterSchema.IdFromRowCol(0, 1));
    m_wndSplitterData.CreateView(0, 0, RUNTIME_CLASS(CItemsListView), CSize(700, 0), pContext);
    m_wndSplitterData.CreateView(0, 1, RUNTIME_CLASS(CDetailHtmlView), CSize(0, 0), pContext);
    ....
}

But I commented "OnSize" is no assert error!

void CControlPanel::OnSize(UINT nType, int cx, int cy)
{
    CFormView::OnSize(nType, cx, cy);
#ifndef _DEBUG
    RECT rect1;
    m_CountList.GetWindowRect(&rect1);
    ScreenToClient(&rect1);
    ....
    m_CountList.MoveWindow(&rect1);
    ....
#endif
}

Search said that adding the following code to solve.

AfxWinInit(::GetModuleHandle(NULL),NULL,::GetCommandLine(),0);

But again this error: f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\appinit.cpp

I think this approach is wrong, ask you how to solve this problem?

Jack An
  • 13
  • 1
  • 4

1 Answers1

1

Looks like OnSize is called before m_CountList's windows is created or not yet attached to Windows window -- has no HWND handle. You get ASSERTs form the framework that you'll try to call API without having a handle. In release build the asserts are removed and that's why you don't get these errors.

The MFC CWnd derived classes have a corresponding Windows window object with HWND on which they call operate attached.

You can test whether your control/windows objects have attached window objects by checking their handle before calling method that call Windows API functions that need window handle.

void CControlPanel::OnSize(UINT nType, int cx, int cy)
{
    CFormView::OnSize(nType, cx, cy);

    if (m_CountList.GetSafeHwnd())
    {
        RECT rect1;
        m_CountList.GetWindowRect(&rect1);
        ScreenToClient(&rect1);
         ....
        m_CountList.MoveWindow(&rect1);
     }
     ....
 }

See also ASSERT(::IsWindow(m_hWnd)) fails in Afxwin2.inl

Mihayl
  • 3,821
  • 2
  • 13
  • 32