Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. View in Dialog Problems

View in Dialog Problems

Scheduled Pinned Locked Moved C / C++ / MFC
c++debuggingperformancequestion
5 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yellowine
    wrote on last edited by
    #1

    I am writing codes to put a view in a dialog. Here are the code to initialize the view in a dialog:BOOL CMyDialog::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here CCreateContext pContext; CWnd* pFrameWnd = this; pContext.m_pCurrentDoc = new CMyDoc(); pContext.m_pNewViewClass = RUNTIME_CLASS(CMyView); CMyView *pView = (CMyView *) ((CFrameWnd*)pFrameWnd)->CreateView(&pContext); ASSERT(pView); pView->ShowWindow(SW_NORMAL); /* * After a view is created, resize that to * have the same size as the dialog. */ CRect rectWindow; GetWindowRect(rectWindow); ScreenToClient(rectWindow); /** * Leave a little space for border and title... */ rectWindow.right += 15; rectWindow.top -= 10; pView->MoveWindow(rectWindow); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE }
    I got two problems: (1) If the CMyView is kind of CHtmlView and if the dialog is displayed within an MFC MDI application, when I clicked the view right after the view is displayed, I got an ASSERT in CView::OnMouseActivate(..). But if the dialog is the main window of a dialog-based MFC app, it works fine. Why?:confused: (2) I use the following code to detect if there is memory leak:void CMyDlgView::OnViewViewInDialog() { // TODO: Add your command handler code here // Declare the variables needed #ifdef _DEBUG CMemoryState oldMemState, newMemState, diffMemState; oldMemState.Checkpoint(); #endif ShowDialog(); #ifdef _DEBUG newMemState.Checkpoint(); if( diffMemState.Difference( oldMemState, newMemState ) ) { TRACE( "Memory leaked!\n" ); } #endif } void CMyDlgView::ShowDialog() { CMyDialog dlg; dlg.DoModal(); }
    if it is a CHtmlView derived view, there is memork leak; however if it is a generic CView derived view, no memory leak. Why?:confused:

    J L 2 Replies Last reply
    0
    • Y yellowine

      I am writing codes to put a view in a dialog. Here are the code to initialize the view in a dialog:BOOL CMyDialog::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here CCreateContext pContext; CWnd* pFrameWnd = this; pContext.m_pCurrentDoc = new CMyDoc(); pContext.m_pNewViewClass = RUNTIME_CLASS(CMyView); CMyView *pView = (CMyView *) ((CFrameWnd*)pFrameWnd)->CreateView(&pContext); ASSERT(pView); pView->ShowWindow(SW_NORMAL); /* * After a view is created, resize that to * have the same size as the dialog. */ CRect rectWindow; GetWindowRect(rectWindow); ScreenToClient(rectWindow); /** * Leave a little space for border and title... */ rectWindow.right += 15; rectWindow.top -= 10; pView->MoveWindow(rectWindow); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE }
      I got two problems: (1) If the CMyView is kind of CHtmlView and if the dialog is displayed within an MFC MDI application, when I clicked the view right after the view is displayed, I got an ASSERT in CView::OnMouseActivate(..). But if the dialog is the main window of a dialog-based MFC app, it works fine. Why?:confused: (2) I use the following code to detect if there is memory leak:void CMyDlgView::OnViewViewInDialog() { // TODO: Add your command handler code here // Declare the variables needed #ifdef _DEBUG CMemoryState oldMemState, newMemState, diffMemState; oldMemState.Checkpoint(); #endif ShowDialog(); #ifdef _DEBUG newMemState.Checkpoint(); if( diffMemState.Difference( oldMemState, newMemState ) ) { TRACE( "Memory leaked!\n" ); } #endif } void CMyDlgView::ShowDialog() { CMyDialog dlg; dlg.DoModal(); }
      if it is a CHtmlView derived view, there is memork leak; however if it is a generic CView derived view, no memory leak. Why?:confused:

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      It is this that got me thinking:

      ((CFrameWnd*)pFrameWnd)->CreateView(...)

      If I'm understading your code, pFrameWnd is a CDialog and not a CFrameWnd. My guess it that this works (when it does) due to pure chance, and it's no wonder weird things happen afterward. Maybe you can take a look at the source code of CFrameWnd::CreateView to try to determine what's going wrong. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      Y 1 Reply Last reply
      0
      • Y yellowine

        I am writing codes to put a view in a dialog. Here are the code to initialize the view in a dialog:BOOL CMyDialog::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here CCreateContext pContext; CWnd* pFrameWnd = this; pContext.m_pCurrentDoc = new CMyDoc(); pContext.m_pNewViewClass = RUNTIME_CLASS(CMyView); CMyView *pView = (CMyView *) ((CFrameWnd*)pFrameWnd)->CreateView(&pContext); ASSERT(pView); pView->ShowWindow(SW_NORMAL); /* * After a view is created, resize that to * have the same size as the dialog. */ CRect rectWindow; GetWindowRect(rectWindow); ScreenToClient(rectWindow); /** * Leave a little space for border and title... */ rectWindow.right += 15; rectWindow.top -= 10; pView->MoveWindow(rectWindow); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE }
        I got two problems: (1) If the CMyView is kind of CHtmlView and if the dialog is displayed within an MFC MDI application, when I clicked the view right after the view is displayed, I got an ASSERT in CView::OnMouseActivate(..). But if the dialog is the main window of a dialog-based MFC app, it works fine. Why?:confused: (2) I use the following code to detect if there is memory leak:void CMyDlgView::OnViewViewInDialog() { // TODO: Add your command handler code here // Declare the variables needed #ifdef _DEBUG CMemoryState oldMemState, newMemState, diffMemState; oldMemState.Checkpoint(); #endif ShowDialog(); #ifdef _DEBUG newMemState.Checkpoint(); if( diffMemState.Difference( oldMemState, newMemState ) ) { TRACE( "Memory leaked!\n" ); } #endif } void CMyDlgView::ShowDialog() { CMyDialog dlg; dlg.DoModal(); }
        if it is a CHtmlView derived view, there is memork leak; however if it is a generic CView derived view, no memory leak. Why?:confused:

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        One of the problems I've come across when using the CMemoryState items is that they tend to take an exact snapshot of memory. Good for some things, bad for others. It showed a memory leak in my code that was really just the fact that I took the snapshot before initializing an array and then comparing it to the array after it was freed and set to empty at the end of the function. The 'leak' went away if I took the snapshot after I did a memset to initialize the array instead of before. My guess is that the class initializes some structures and the difference is shown as a leak. If you use a trial version of one of the other memory checkers (BoundsChecker, Insure, etc...) then it might help determine if it's a real leak. Good luck with the project.

        Y 1 Reply Last reply
        0
        • L Lost User

          One of the problems I've come across when using the CMemoryState items is that they tend to take an exact snapshot of memory. Good for some things, bad for others. It showed a memory leak in my code that was really just the fact that I took the snapshot before initializing an array and then comparing it to the array after it was freed and set to empty at the end of the function. The 'leak' went away if I took the snapshot after I did a memset to initialize the array instead of before. My guess is that the class initializes some structures and the difference is shown as a leak. If you use a trial version of one of the other memory checkers (BoundsChecker, Insure, etc...) then it might help determine if it's a real leak. Good luck with the project.

          Y Offline
          Y Offline
          yellowine
          wrote on last edited by
          #4

          Anonymous wrote: My guess is that the class initializes some structures and the difference is shown as a leak. If you use a trial version of one of the other memory checkers (BoundsChecker, Insure, etc...) then it might help determine if it's a real leak. Thanks a lot. Your argument is what I have been suspecting. I would try other memory check programs for this example. Hope you are right.

          1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            It is this that got me thinking:

            ((CFrameWnd*)pFrameWnd)->CreateView(...)

            If I'm understading your code, pFrameWnd is a CDialog and not a CFrameWnd. My guess it that this works (when it does) due to pure chance, and it's no wonder weird things happen afterward. Maybe you can take a look at the source code of CFrameWnd::CreateView to try to determine what's going wrong. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            Y Offline
            Y Offline
            yellowine
            wrote on last edited by
            #5

            I have tried other method to create a view using something like: CView *pView = pRuntimeClass->CreateObject(); pView->Create(...); It got the same assert. However if I add (Overide) a message handler of OnMouseActive(..)of CMyView and just call CWnd::OnMouseActivate(..) within that handler, the ASSERT goes away. Odd!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups