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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. MessageBox Not Displayed from Destructor?

MessageBox Not Displayed from Destructor?

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingquestionlearning
4 Posts 2 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.
  • J Offline
    J Offline
    Justin Cooke
    wrote on last edited by
    #1

    Greetings, I'm trying to display simple debug messages from the constructor and destructor of a class (we'll call it MyClass.) I have a simple CDialog based project and if I declare a member object of MyClass in the main dialog class definition, I expect to see the "MyClass::MyClass()" message box when starting the app and the "MyClass::~MyClass()" when closing the app. The destructor message is never displayed. I've used ::MessageBox() and AfxMessageBox() and I've tried NULL and ::GetDesktopWindow() as the hWnd param to ::MessageBox. Of course, if I just locally create and destroy the object in a function body (such as a button click event), then I see both messages. Can anyone clue me in as to why this is happening? Does it have something to do with the fact that the main dialog has already been destroyed when the member MyClass object's destructor is called? Thanks, Justin

    D 1 Reply Last reply
    0
    • J Justin Cooke

      Greetings, I'm trying to display simple debug messages from the constructor and destructor of a class (we'll call it MyClass.) I have a simple CDialog based project and if I declare a member object of MyClass in the main dialog class definition, I expect to see the "MyClass::MyClass()" message box when starting the app and the "MyClass::~MyClass()" when closing the app. The destructor message is never displayed. I've used ::MessageBox() and AfxMessageBox() and I've tried NULL and ::GetDesktopWindow() as the hWnd param to ::MessageBox. Of course, if I just locally create and destroy the object in a function body (such as a button click event), then I see both messages. Can anyone clue me in as to why this is happening? Does it have something to do with the fact that the main dialog has already been destroyed when the member MyClass object's destructor is called? Thanks, Justin

      D Offline
      D Offline
      Diddy
      wrote on last edited by
      #2

      If this is an MFC app, make sure you don't have the line m_pMainWnd = &dlg in CYouWinApp::InitInstance.

      J 1 Reply Last reply
      0
      • D Diddy

        If this is an MFC app, make sure you don't have the line m_pMainWnd = &dlg in CYouWinApp::InitInstance.

        J Offline
        J Offline
        Justin Cooke
        wrote on last edited by
        #3

        Thanks. That was the problem. Does that work because the main dialog isn't really the main window, so if you set m_pMainWnd to the dialog, when you kill the dialog, the app prematurely teminates? (I read the MSDN docs, but I'm not totally clear on it yet.) Thanks, Justin

        D 1 Reply Last reply
        0
        • J Justin Cooke

          Thanks. That was the problem. Does that work because the main dialog isn't really the main window, so if you set m_pMainWnd to the dialog, when you kill the dialog, the app prematurely teminates? (I read the MSDN docs, but I'm not totally clear on it yet.) Thanks, Justin

          D Offline
          D Offline
          Diddy
          wrote on last edited by
          #4

          Er well it works because of the way DoModal works. Each (UI) thread has a message pump as im sure u know, and in MFC, that pump is usually started by returing true from InitInstance. When you call DoModal on a dialog - you want that call to block until the dialog is closed - and you also want to continue to pump messages while waiting. MFC does that by running another message pump within DoModal, that runs until EndDialog is called and then passes control back out to the "outer" pump. That all apples if your not using a dialog for you "main" window. When the message pump in DoModal comes across a WM_QUIT message, it handles it differently. Its the outer pump that must process this quit message, so it reposts another WM_QUIT message and then exits it's loop. If you have m_pMainWnd set to anything other than NULL, then the WM_QUIT message is reposted. Once a thread has received WM_QUIT, calling GetMessage/PeekMessage will do nothing. Internally, a MessageBox also pumps and process messages - so if you have a non-null m_pMainWnd, your message box will start it's own message pump, and will get the WM_QUIT message which was reposted - and exit. If you set m_pMainWnd to NULL, the WM_QUIT message is esentially swollowd by the message pump in DoModal - even though the DoModal loop exits, the WM_QUIT doesn't get reposted, and hence the message box doesn't pick it up. The app is then exited via returing false at the end of InitInstance.

          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