Mark Stahler
Mark Stahler

Reputation: 4246

MFC Assertion fails after upgrading from VC++ 6 to MSVC 2005

Is it normal to have to update assertions after upgrading the compiler from VC++ 6 to MSVC 2005? I have the following function which works without triggering the assertion in Visual Studio 6 but anything newer it fails.

void CMainFrame::OnUpdateGraphValue (CCmdUI* pCmdUI) {

    BOOL bMax;

    CMDIChildWnd *child = MDIGetActive (&bMax);
    if (child)
    {
        if (child->IsKindOf (RUNTIME_CLASS (CGaugeChildFrame)))
        {
            CGaugeView *pView = (CGaugeView *) child->GetActiveView ();
            if (pView->wndActive)
            {
                ASSERT (pView->IsKindOf (RUNTIME_CLASS (CGaugeView)));
                pCmdUI->Enable (TRUE);
                return;
            }
        }
        if (child->IsKindOf (RUNTIME_CLASS (CGarterChildFrame)))
        {
            CGarterView *pView = (CGarterView *) child->GetActiveView ();
            if (pView->wndGraphics)
            {
                ASSERT (pView->IsKindOf (RUNTIME_CLASS (CGarterView)));
                pCmdUI->Enable (TRUE);
                return;
            }
        }
    }

pCmdUI->Enable (FALSE); }

The failure occurs on line ASSERT (pView->IsKindOf (RUNTIME_CLASS (CGaugeView))); When I click print preview the type is not CGaugeView but CPreviewView.

Can someone please shed some light on this for me? Thanks

Upvotes: 1

Views: 264

Answers (1)

Ben
Ben

Reputation: 35613

It's not valid to cast to a type before you have checked the type is compatible.

So you need to do:

   if(child->GetActiveView ()->IsKindOf(RUNTIME_CLASS(CGaugeView)))
   {
        CGaugeView *pView = (CGaugeView *) child->GetActiveView ();

As to why has this behaviour changed, I don't know. Maybe before you were ignoring the asserts? Maybe you didn't try a debug built?

Or maybe the print preview architecture has changed in version 7? Maybe there was no pView->wndGraphics in print preview mode in the previous version, so the code path never got triggered.

However since you aren't using the code path for anything maybe just dump it.

Upvotes: 2

Related Questions