I've developing MFC-MDI applications and this application has tabbed document style.
I want to activate each view programmatically and using CMainFrame::SetActiveView method like below.

void CMyView::OnShowWindow( BOOL bShow ... )
{
CView::OnShowWindow( bShow, ... );

CMainFrame *mf = (CMainFrame*)AfxGetMainWnd();
mf->SetActiveView(this);
}

This code makes a view active programatically but has a critical problem that the application pops up the error about the activation of the context when I close a document.

How can I solve this problem?