Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Deleting views in MDI applications

76 views
Skip to first unread message

Marco Hahn

unread,
Nov 29, 2001, 9:12:34 AM11/29/01
to
Hi,

I created different views of one document using a MultiDocTemplate.
How do I delete all the views automatically when one view is closed?
Is there another way to create these different views of one and the same
document dynamically?

Thanks
Marco

Scot T Brennecke

unread,
Nov 29, 2001, 1:09:44 PM11/29/01
to
OnCloseDocument

Marco Hahn

unread,
Nov 30, 2001, 3:09:16 AM11/30/01
to
That's the way I tried it, but I get an access violation while destroying the frame (pFrame->DestroyWindow() in void CDocument::OnCloseDocument()).
See bold face (this occurs after deleting the first frame).
 
 

void CDocument::OnCloseDocument()
 // must close all views now (no prompting) - usually destroys this
{
 // destroy all frames viewing this document
 // the last destroy may destroy us
 BOOL bAutoDelete = m_bAutoDelete;
 m_bAutoDelete = FALSE;  // don't destroy document while closing views
 while (!m_viewList.IsEmpty())
 {
  // get frame attached to the view
  CView* pView = (CView*)m_viewList.GetHead();
  ASSERT_VALID(pView);
  CFrameWnd* pFrame = pView->GetParentFrame();
  ASSERT_VALID(pFrame);

  // and close it
  PreCloseFrame(pFrame);
  pFrame->DestroyWindow();
   // will destroy the view as well
 }
 m_bAutoDelete = bAutoDelete;

 // clean up contents of document before destroying the document itself
 DeleteContents();

 // delete the document if necessary
 if (m_bAutoDelete)
  delete this;
}
 
 
 
 

Scot T Brennecke schrieb:

OnCloseDocument

Scot T Brennecke

unread,
Dec 1, 2001, 5:50:02 PM12/1/01
to
Then you need to discover what is causing the AV. There is nothing inherent in
closing frames that would cause an AV, so it must be something in your code.
When the AV occurs, the debugger should take you to the code that is causing
it. Where is that?

Marco Hahn

unread,
Dec 3, 2001, 5:55:16 AM12/3/01
to
These are the steps in my program:

1.) Definition of the template and entry in the m_MapView, which is a map storing
the pointers to my templates:

BOOL CGuiApp::InitInstance()
{
.
.
.
pDocTemplate = new CMultiDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CGuiDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CDimMatrixView));
AddDocTemplate(pDocTemplate);
m_MapView.SetAt("CDimMatrixView", pDocTemplate);
.
.
.
}

2.) Creation of the "DimMatrixView" in my "GuiView" class (using a map-lookup to
retrieve the template):

void CGuiView::OnDisplayDimensionalmatrix()
{
.
.
.
CGuiApp *pApp = (CGuiApp*)AfxGetApp();
CMultiDocTemplate* pa;
if (pApp->m_MapView.Lookup("CDimMatrixView", (CObject *&) pa ))
{
CFrameWnd *pFrame = pa->CreateNewFrame(GetDocument(), GetParentFrame());

pFrame->InitialUpdateFrame(GetDocument(), TRUE);
}
.
.
.
}

3.) Deleting all views of one document out of my "GuiView" class:

void CGuiView::OnDestroy()
{
CScrollView::OnDestroy();

// TODO: Code für die Behandlungsroutine für Nachrichten hier einfügen

GetDocument()->OnCloseDocument();
}

4.) Exception in OnCloseDocument():
Destroying the first frame is no problem.
In the second cycle of line 8-20, pView in line 11 is the same pointer as in
the first cycle.
Therefore I get a NULL pFrame pointer in line 13, which causes the exception in
line 18.

1 void CDocument::OnCloseDocument()
2 // must close all views now (no prompting) - usually destroys this
3 {
4 // destroy all frames viewing this document
5 // the last destroy may destroy us
6 BOOL bAutoDelete = m_bAutoDelete;
7 m_bAutoDelete = FALSE; // don't destroy document while closing views
8 while (!m_viewList.IsEmpty())
9 {
10 // get frame attached to the view
11 CView* pView = (CView*)m_viewList.GetHead();
12 ASSERT_VALID(pView);
13 CFrameWnd* pFrame = pView->GetParentFrame();
14 ASSERT_VALID(pFrame);
15
16 // and close it
17 PreCloseFrame(pFrame);
18 pFrame->DestroyWindow();
19 // will destroy the view as well
20 }
21 m_bAutoDelete = bAutoDelete;
22
23 // clean up contents of document before destroying the document itself
24 DeleteContents();
25
26 // delete the document if necessary
27 if (m_bAutoDelete)
28 delete this;
29 }

I hope, this is enough to give you an idea of my problem.
Any suggestions for fixing it?

Scot T Brennecke schrieb:

Scot T Brennecke

unread,
Dec 3, 2001, 1:56:15 PM12/3/01
to
I think the problem may lie in that the view is still in the document's view
list when OnCloseDocument is called. The view removes itself from the document
in its destructor, not in OnDestroy.
if (m_pDocument != NULL)
m_pDocument->RemoveView(this);
So, you probably need to remove it prior to calling OnCloseDocument.

Marco Hahn

unread,
Dec 4, 2001, 3:11:24 AM12/4/01
to
Yippie, it works great!
The only modification is to store m_pDocument in a temporal variable before you
call RemoveView (RemoveView sets the view’s document pointer to NULL) .

Thanks a bomb!
Marco


Scot T Brennecke schrieb:

0 new messages