The Phantom Bug: Why doesn’t MessageBox work from my WM_NCDESTROY handler?


Adrian McCarthy ran into a problem where MessageBox didn't work when called from a WM_NCDESTROY handler. You already know how to solve this; you just have to connect the dots. See if you can do it on your own before the answer is revealed.

Comments (5)
  1. Mike Dimmick says:

    He’d already called PostQuitMessage, so the modal message loop set up by MessageBox just quit out straight away (and presumably reposted the quit flag to the message queue). MessageBox has to return something, so it returns IDABORT. (Returning 0, IDOK, would be bad.)

  2. Alexandre Grigoriev says:

    ASSERT is not particularly well designed. It should run its message box in a separate thread instead, otherwise any WM_TIMER messages (and other posted messages, too) will get handled and might then cause unexpected recursion. And G-d forbid you ever get ASSERT in WM_PAINT handler context…

  3. Joe Butler says:

    Alexandre,

    If your program is asserting, surely the battle is lost at that point, so does it really matter?

  4. Alexandre Grigoriev says:

    Joe Butler,

    How do you debug a program that recursively spews ASSERT dialogs, without waiting for you to click on them? Showing a modal dialog box also changes the main window state, which you may not be able to handle. If the dialog were created in a separate thread, while keeping the offending thread blocked, it would be better, as the offending code state would be preserved.

  5. Joe Butler says:

    Alexandre Grigoriev,

    The way I do it is to set breakpoints to home into the general area of such a recursive or itterative assert.  Then it’s normally obvious what’s going on and it can be fixed.  If you want a debugging message like you describe, it’s not that difficult to knock one up – I have a logger that runs a gui in a separate thread that I’ve found quite useful, and also a large ‘tooltip’ window that i can write status info into that gets updated on a separate thread.  But regardless, I don’t think it’s fair to say that the assert box is ‘badly designed’.  It does what it does and it does it the way it does it.

Comments are closed.

Skip to main content