none
MFC CDocTemplate CreateNewFrame

    General discussion


  • Hi All,

     

    I have come across a problem when creating frames using CDocTemplate CreateNewFrame.

     

    Here's the line of code. Note pDoc is a CDocument pointer.

     

        //View hasn't been created yet or create multiple instances

        CMDIChildWnd* pNewFrame = (CMDIChildWnd*)pTemplate->CreateNewFrame(pDoc, NULL);

     

    Here's the problem. I need to create multiple instances of the same type of frame. It works for the first 9 frames then it crashes when creating the 10th one.

     

    I've traced the calls to the following found in winmdi.cpp:

     

    HWND hWnd = (HWND)::SendMessage(pParentWnd->m_hWndMDIClient,

            WM_MDICREATE, 0, (LPARAM)&mcs);

     

    After it makes this call I see, "Windows has triggered a breakpoint .... This may be due to corruption of the heap, and indicates a bug."

     

    In a release build, it behaves a little bit differently.  CreateNewFrame returns a null pointer (after creating the 10th frame) at which time I prompt the user to close some of the previously created windows, in order to open (create) new ones. This also leads to a crash eventually... either right away or after a few minutes.

     

    At first I thought this might be a resources issue, but the virtual ram usage looked 'OK', about 1.2G (windows XP.)

     

    It seems odd to me that this occurs after a set number. Is there some sort of resource limit that I am not aware of?

     

    Thanks in advance for any help.

    • Edited by SDDirtySox Friday, April 24, 2009 10:11 PM
    • Changed type Nancy Shao Thursday, April 30, 2009 3:17 AM op does not follow up
    • Changed type Nancy Shao Thursday, April 30, 2009 3:18 AM op does not follow up
    Friday, April 24, 2009 10:02 PM

All replies

  • Has anyone seen this problem?
    Monday, April 27, 2009 4:23 PM
  • I'm not familiar with the procedures for creating an MDI application in MFC. But, in general, an MDI application has one Frame Window, with just one child, the Client Window, and the application documents which are children of the Client Window.

    When you say "I need to create multiple instances of the same type of frame. It works for the first 9 frames then it crashes when creating the 10th one", do you mean to say that you need 10 MDI applications running concurrently in the machine, or do you want to create 10 documents in the same MDI application ?

    Monday, April 27, 2009 4:44 PM
  • Thanks for the info Belloc. 10 documents in the same MDI application.
    Monday, April 27, 2009 4:48 PM
  • Do you know how the MDICREATESTRUCT mcs is being initialized in the call

    HWND hWnd = (HWND)::SendMessage(pParentWnd->m_hWndMDIClient,

            WM_MDICREATE, 0, (LPARAM)&mcs);

    Monday, April 27, 2009 5:14 PM
  • Do you know how the MDICREATESTRUCT mcs is being initialized in the call

    HWND hWnd = (HWND)::SendMessage(pParentWnd->m_hWndMDIClient,

            WM_MDICREATE, 0, (LPARAM)&mcs);

    Belloc, this is the code that initializes mcs before it's passed to SendMessage. It's in C:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\src\mfc\winmdi.cpp.

    Thanks again.

    BOOL CMDIChildWnd::Create(LPCTSTR lpszClassName,
        LPCTSTR lpszWindowName, DWORD dwStyle,
        const RECT& rect, CMDIFrameWnd* pParentWnd,
        CCreateContext* pContext)
    {
        if (pParentWnd == NULL)
        {
            CWinThread *pThread = AfxGetThread();
            ENSURE_VALID(pThread);
            CWnd* pMainWnd = pThread->m_pMainWnd;
            ENSURE_VALID(pMainWnd);
            ASSERT_KINDOF(CMDIFrameWnd, pMainWnd);
            pParentWnd = (CMDIFrameWnd*)pMainWnd;
        }
        ASSERT(::IsWindow(pParentWnd->m_hWndMDIClient));

        // insure correct window positioning
        pParentWnd->RecalcLayout();

        // first copy into a CREATESTRUCT for PreCreate
        CREATESTRUCT cs;
        cs.dwExStyle = 0L;
        cs.lpszClass = lpszClassName;
        cs.lpszName = lpszWindowName;
        cs.style = dwStyle;
        cs.x = rect.left;
        cs.y = rect.top;
        cs.cx = rect.right - rect.left;
        cs.cy = rect.bottom - rect.top;
        cs.hwndParent = pParentWnd->m_hWnd;
        cs.hMenu = NULL;
        cs.hInstance = AfxGetInstanceHandle();
        cs.lpCreateParams = (LPVOID)pContext;

        if (!PreCreateWindow(cs))
        {
            PostNcDestroy();
            return FALSE;
        }
        // extended style must be zero for MDI Children (except under Win4)
        ASSERT(cs.hwndParent == pParentWnd->m_hWnd);    // must not change

        // now copy into a MDICREATESTRUCT for real create
        MDICREATESTRUCT mcs;
        mcs.szClass = cs.lpszClass;
        mcs.szTitle = cs.lpszName;
        mcs.hOwner = cs.hInstance;
        mcs.x = cs.x;
        mcs.y = cs.y;
        mcs.cx = cs.cx;
        mcs.cy = cs.cy;
        mcs.style = cs.style & ~(WS_MAXIMIZE | WS_VISIBLE);
        mcs.lParam = (LPARAM)cs.lpCreateParams;

        // create the window through the MDICLIENT window
        AfxHookWindowCreate(this);
        HWND hWnd = (HWND)::SendMessage(pParentWnd->m_hWndMDIClient,
            WM_MDICREATE, 0, (LPARAM)&mcs);
    Monday, April 27, 2009 8:52 PM
  • Could you post your code for pNewFrame->OnCreate()

    Monday, April 27, 2009 9:28 PM
  • Hi,

    We are changing the issue type to “General Discussion” because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question” by opening the Options list at the top of the post editor window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.

    Thank you!

    Best Regards,
    Nancy


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, April 30, 2009 3:17 AM