views:

121

answers:

2

I have an MFC application that spawns a number of different worker threads and is compiled with VS2003.

When calling CTreeCtrl::GetItemState() I'm occasionally getting a debug assertion dialog popup. I'm assuming that this is because I've passed in a handle to an invalid item but this isn't my immediate concern.

My concern is: From my logs, it looks as though the MFC thread continues to service a number of windows messages whilst the assert dialog is being displayed. I thought the assert dialog was modal so I was wondering if this was even possible?

+1  A: 

Dialogs (even a messagebox) need to pump the message queue, even if they're modal. Otherwise how would they know you clicked on the "OK" button?

If you need to stop everything when an assert triggers it's usually not too difficult to write your own implementation of assert() (or ASSERT() or whatever) that will break into the debugger instead of displaying a messagebox that asks if you want to break into the debugger (maybe only if it determines that the debugger is attached).

Michael Burr
+1  A: 

The message box that shows the assertion failure has a message pump for its own purposes. But it'll dispatch all messages that come in, not just those for the message box (otherwise things could get blocked).

With a normal modal dialog, this isn't a problem because the parent window is typically disabled for the duration of the dialog.

The code that launches the assertion dialog must've failed to figure out the parent window, and thus it wasn't disabled. This can happen if your main window isn't the active window at the time of the assertion. Other things can go wrong as well.

You can change how Visual Studio's C run-time library reports assertion failures with _CrtSetReportMode. You can make it stop in the debugger and/or log to the output window instead of trying to show the dialog.

Adrian McCarthy
Thanks for the explanation, that sounds perfectly plausible.I like the idea of using _CrtSetReportMode to get the software to do something better than throw up a dialog but unfortunately it's running on an independent PC without Visual Studio installed.
Ian Hickman
I assumed this was on your development machine since you have assertions enabled.
Adrian McCarthy