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. modeless dialog in wtl

modeless dialog in wtl

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
8 Posts 5 Posters 0 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.
  • O Offline
    O Offline
    ov
    wrote on last edited by
    #1

    I have a CDialogImpl-based class, implementing a modeless dialog. My application can have a number of such dialogs, so I'm using this code to create one:

    CMyDialog *pDlg = new CMyDialog;
    pDlg->Create(NULL);
    pDlg->ShowWindow(SW_SHOW);

    When user will press the close-button in the upper right corner of my dialog - I must destroy the object and the window, so I'm using DestroyWindow(); in WM_CLOSE handler and delete this; in OnFinalMessage(). The problem is that the program falls with ASSERT(pThis->m_pCurrentMsg == &msg) in atlwin.h, line 2281. Btw, if I close the main application - these dialogs are destroying without any ASSERTs... How can I delete the class after the window have been destroyed? With the best regards, Vitaly.

    C T 2 Replies Last reply
    0
    • O ov

      I have a CDialogImpl-based class, implementing a modeless dialog. My application can have a number of such dialogs, so I'm using this code to create one:

      CMyDialog *pDlg = new CMyDialog;
      pDlg->Create(NULL);
      pDlg->ShowWindow(SW_SHOW);

      When user will press the close-button in the upper right corner of my dialog - I must destroy the object and the window, so I'm using DestroyWindow(); in WM_CLOSE handler and delete this; in OnFinalMessage(). The problem is that the program falls with ASSERT(pThis->m_pCurrentMsg == &msg) in atlwin.h, line 2281. Btw, if I close the main application - these dialogs are destroying without any ASSERTs... How can I delete the class after the window have been destroyed? With the best regards, Vitaly.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Surely if you've called DestroyWindow then that is all you need to do. Have you traced back to see what was called to cause the ASSERT? I'm betting on delete this. Christian #include "std_disclaimer.h" The careful application of terror is also a form of communication. Eagles may soar, but weasels don't get sucked into jet engines.

      O 1 Reply Last reply
      0
      • O ov

        I have a CDialogImpl-based class, implementing a modeless dialog. My application can have a number of such dialogs, so I'm using this code to create one:

        CMyDialog *pDlg = new CMyDialog;
        pDlg->Create(NULL);
        pDlg->ShowWindow(SW_SHOW);

        When user will press the close-button in the upper right corner of my dialog - I must destroy the object and the window, so I'm using DestroyWindow(); in WM_CLOSE handler and delete this; in OnFinalMessage(). The problem is that the program falls with ASSERT(pThis->m_pCurrentMsg == &msg) in atlwin.h, line 2281. Btw, if I close the main application - these dialogs are destroying without any ASSERTs... How can I delete the class after the window have been destroyed? With the best regards, Vitaly.

        T Offline
        T Offline
        Todd Smith
        wrote on last edited by
        #3

        The problem is this code near the bottom of that function in atlwin.h

        pThis->OnFinalMessage(hWnd);

        You can't call delete this from a function that's being called by this. I hope that makes sense. The way I would solve this is to create a WM_USER function which does the delete from the main app such as:

        #define WM_USER_DELETE_DIALOG WM_USER+1

        // call deleteme from onclose after calling destory
        CMyDialog::DeleteMe()
        {
        // can add info to describe which dialog to delete as a param
        // must be post and not send
        PostMessage(parent, WM_USER_DELETE_DIALOG , 0, 0);
        }

        CApp::OnDeleteDialog()
        {
        delete pDlg;
        }

        M O 2 Replies Last reply
        0
        • T Todd Smith

          The problem is this code near the bottom of that function in atlwin.h

          pThis->OnFinalMessage(hWnd);

          You can't call delete this from a function that's being called by this. I hope that makes sense. The way I would solve this is to create a WM_USER function which does the delete from the main app such as:

          #define WM_USER_DELETE_DIALOG WM_USER+1

          // call deleteme from onclose after calling destory
          CMyDialog::DeleteMe()
          {
          // can add info to describe which dialog to delete as a param
          // must be post and not send
          PostMessage(parent, WM_USER_DELETE_DIALOG , 0, 0);
          }

          CApp::OnDeleteDialog()
          {
          delete pDlg;
          }

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          That's odd, though. The docs on CDialogImpl::OnFinalMessage() say:

          Called after receiving the last message (typically WM_NCDESTROY). Note that if you want to automatically delete your object upon the window destruction, you can call delete this; here.

          --Mike-- http://home.inreach.com/mdunn/ "Make sure that if you are using a blow torch that you don't set anything on fire."   -- Chris Maunder

          O 1 Reply Last reply
          0
          • M Michael Dunn

            That's odd, though. The docs on CDialogImpl::OnFinalMessage() say:

            Called after receiving the last message (typically WM_NCDESTROY). Note that if you want to automatically delete your object upon the window destruction, you can call delete this; here.

            --Mike-- http://home.inreach.com/mdunn/ "Make sure that if you are using a blow torch that you don't set anything on fire."   -- Chris Maunder

            O Offline
            O Offline
            ov
            wrote on last edited by
            #5

            I've read the same in MFC documentation. And I've thought that atl works the same way but it doesn't :( I don't like idea with WM_USER-based message but it seems to be the only way to solve this problem. With the best regards, Vitaly.

            1 Reply Last reply
            0
            • C Christian Graus

              Surely if you've called DestroyWindow then that is all you need to do. Have you traced back to see what was called to cause the ASSERT? I'm betting on delete this. Christian #include "std_disclaimer.h" The careful application of terror is also a form of communication. Eagles may soar, but weasels don't get sucked into jet engines.

              O Offline
              O Offline
              ov
              wrote on last edited by
              #6

              I don't think that DestroyWindow() is enough. Cause my destructor is not called in such case. With the best regards, Vitaly.

              1 Reply Last reply
              0
              • T Todd Smith

                The problem is this code near the bottom of that function in atlwin.h

                pThis->OnFinalMessage(hWnd);

                You can't call delete this from a function that's being called by this. I hope that makes sense. The way I would solve this is to create a WM_USER function which does the delete from the main app such as:

                #define WM_USER_DELETE_DIALOG WM_USER+1

                // call deleteme from onclose after calling destory
                CMyDialog::DeleteMe()
                {
                // can add info to describe which dialog to delete as a param
                // must be post and not send
                PostMessage(parent, WM_USER_DELETE_DIALOG , 0, 0);
                }

                CApp::OnDeleteDialog()
                {
                delete pDlg;
                }

                O Offline
                O Offline
                ov
                wrote on last edited by
                #7

                Yes, I've though about this way. The problem is that I will have many windows like this. Different windows, different classes... So I must implement some kind of window manager for them. But I still hope that delete this; will work somewhere :) With the best regards, Vitaly.

                C 1 Reply Last reply
                0
                • O ov

                  Yes, I've though about this way. The problem is that I will have many windows like this. Different windows, different classes... So I must implement some kind of window manager for them. But I still hope that delete this; will work somewhere :) With the best regards, Vitaly.

                  C Offline
                  C Offline
                  CodeGuy
                  wrote on last edited by
                  #8

                  From the WTL newsgroup (sign up! -- http://groups.yahoo.com/group/wtl) From: "Tim Smith" Date: Thu Jan 18, 2001 11:41 am Subject: Re: DestroyWindow and OnFinalMessage ? Here is a basic roadmap of what you need to do. First, inside your dialog box class, you need make a new version of DialogProc. I just copied the DialogProc from ATL. Next, add a flag to your class called m_fDeferDelete. Initialize it to false. Inside your new DialogProc, in the WM_NCDESTROY clause after the call to OnFinalMessage, add the following lines: if (pThis ->m_pCurrentMsg == NULL) delete pThis; else pThis ->m_fDeferDelete = true Then in your new DialogProc, after the call to ProcessWindowMessage, add the code: if (pThis ->m_fDeferDelete && pOldMsg == NULL) { delete pThis; return FALSE; } There might be a better way to do it, but this will work. In my real code, I created a new base class that included an auto delete flag. All my dialogs are based on this class. If the auto delete flag is not set, then the dialog works normally. If it is set, then the class self deletes as expected.

                  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