Re: Pointer to Document in MDI application - Problem

From: Alan Compton (alan.compton_at_g4tech.co.uk)
Date: 08/10/04

  • Next message: Alan Compton: "Re: strange tree ctrl behavior"
    Date: 10 Aug 2004 01:25:04 -0700
    
    

    Mike,

    What you have is a common problem. Many applications require a single
    document (usually a database connection) and many views. It seems
    surprising that the AppWizard missed this option out.

    The KB article Q196832 describes a similar problem.

    Read carefully through what I said earlier. Its all in there.
    m_bAutoDelete is the key as it stops the document from being deleted.

    One other thing: I create the 'only' document in the App's
    OnInitInstance, after the document template has been created (code put
    in by the Wizard), I do this:

        m_pDoc = (CFDoc*) pDocTemplate->CreateNewDocument();

    Alan.

    Mike <Mike@home.net> wrote in message news:<v4neh0901319k5lsn6bljrqk8rk8clk0lq@4ax.com>...
    > Hi Alan,
    > You have helped me to realise what the problem is.
    > You're right, I've added an m_pDoc member to my app class. I
    > initialise this pointer in my document's OnNewDocument() method, like
    > so:
    >
    > // set pointer to app's doc
    > ((CFApp*)AfxGetApp())->m_pDoc = this;
    >
    > Therefore, when I close the current document, the app's m_pDoc is no
    > longer pointing at a valid document, even though the default doc is
    > still open in the application. So CFDocument::GetDoc fails wehn
    > called.
    >
    > I'm not sure what the best way to solve this would be.
    > Thanks,
    > Mike
    >
    > On 9 Aug 2004 02:36:15 -0700, alan.compton@g4tech.co.uk (Alan Compton)
    > wrote:
    >
    > >Mike,
    > >
    > >I'm assuming that the CFApp::m_pDoc member is one that you've added
    > >and you create a document when the app is initialised.
    > >
    > >The problem is that MFC keed a pointer to the document created for
    > >each MDI child view. When the view closes, the document is deleted.
    > >Even if you have kept a copy of the pointer to the document in m_pDoc,
    > >the object it self is deleted so your left pointing to memory you no
    > >longer 'own'.
    > >
    > >The secret lies in the CDocument member, m_bAutoDelete.
    > >
    > >In your document class constructor, set m_bAutoDelete to FALSE. This
    > >will stop MFC from deleting the document.
    > >
    > >Override CDocument::OnCloseDocument(); and set it back to TRUE before
    > >calling the base class implmentation.
    > >
    > >At some suitable closing point (like mainframe OnClose) you will have
    > >to get your app and call pApp->m_pDoc->OnCloseWindow(); This gives MFC
    > >the change to delete the document.
    > >
    > >Alan
    > >
    > >Mike <Mike@home.net> wrote in message news:<0gpbh09nflgvv3cjabdp2nf5ril84nsrr3@4ax.com>...
    > >> Hi,
    > >> In my MDI application, a default document is created when the app. is
    > >> started. If I open then close a saved document, the default doc is
    > >> still there. However, if i then try to work with the default document,
    > >> I get an Access Violation when a dialog window tries to get a pointer
    > >> to the doc by calling it's GetDoc() function:
    > >>
    > >> CFDocument* CFDocument::GetDoc()
    > >> {
    > >>
    > >> CMainFrame* pMFrame = (CMainFrame*)(AfxGetApp()->m_pMainWnd);
    > >>
    > >> if(!pMFrame)
    > >> return NULL;
    > >>
    > >> CChildFrame* pChild = (CChildFrame*) pMFrame->MDIGetActive();
    > >>
    > >> if(!pChild)
    > >> return NULL;
    > >>
    > >> CFApp* pApp = (CFApp*) AfxGetApp();
    > >> CFDocument* pDoc = pApp->m_pDoc;
    > >> // forced to do it this way, because GetActiveDocument searches via
    > >> active view,
    > >> // and we don't always have one...
    > >>
    > >> if(!pDoc)
    > >> return NULL;
    > >>
    > >> if(!pDoc->IsKindOf(RUNTIME_CLASS(CFDocument))) //**Call stack points
    > >> here after crash**
    > >> return NULL;
    > >>
    > >> return (CFDocument*)pDoc;
    > >>
    > >> }
    > >>
    > >> I have no problem performing the same operation if I do it before I
    > >> have opened and closed a saved document. I can also create a New
    > >> document and that gives no troubles. Any idea why opening and closing
    > >> a saved document gives this problem?
    > >>
    > >> Thanks,
    > >> Mike


  • Next message: Alan Compton: "Re: strange tree ctrl behavior"

    Relevant Pages

    • Re: newbie troubles - creating an array dynamically. best practices.
      ... > dynamic array, I wrote a function, that has to do the job. ... It would allocate new memory, no pointers are being created in line A. You ... > in order to make it initialize the pointer c at one and the same address ... If you initialise pointer c, and then never assign to it again it will ...
      (comp.lang.cpp)
    • GC create/copy problem
      ... Or does XCopyGC actually initialise the structure with defaults for me? ... Display *display = XOpenDisplay; ... makes integer from pointer without cast." ...
      (comp.windows.x)
    • Re: Debugging standard C library routines
      ... I wouldn't go so far as to advise to initialise _every_ pointer. ... Such stupid advice leads, when followed, ... In Debug Mode, maybe. ...
      (comp.lang.c)
    • Re: NULL
      ... > What I do is When ever I declare a pointer, I Initialise with the NULL; ... zero value has the same meaning as with NULL. ...
      (alt.comp.lang.learn.c-cpp)

    Loading