views:

133

answers:

5

Hello! I am developing a simple WinAPI application and started from writing my own assertion system.

I have a macro defined like ASSERT(X) which would make pretty the same thing as assert(X) does, but with more information, more options and etc.

At some moment (when that assertion system was already running and working) I realized there is a problem.

Suppose I wrote a code that does some action using a timer and (just a simple example) this action is done while handling WM_TIMER message. And now, the situation changes the way that this code starts throwing an assert. This assert message would be shown every TIMER_RESOLUTION milliseconds and would simply flood the screen.

Options for solving this situation could be:

1) Totally pause application running (probably also, suspend all threads) when the assertion messagebox is shown and continue running after it is closed

2) Make a static counter for the shown asserts and don't show asserts when one of them is already showing (but this doesn't pause application)

3) Group similiar asserts and show only one for each assert type (but this also doesn't pause application)

4) Modify the application code (for example, Get / Translate / Dispatch message loop) so that it suspends itself when there are any asserts. This is good, but not universal and looks like a hack.

To my mind, option number 1 is the best. But I don't know any way how this can be achieved. What I'm seeking for is a way to pause the runtime (something similiar to Pause button in the debugger). Does somebody know how to achieve this?

Also, if somebody knows an efficient way to handle this problem - I would appreciate your help. Thank you.

A: 

Use the MessageBox function. This will block until the user clicks "ok". After this is done, you could choose to discard extra assertion failure messages or still display them as your choice.

DeadMG
It won't block the app from processing its message loop, if it did the parent window would never redraw.
Alex K.
Unfortunately, this doesn't help. You could try generating windows via `MessageBox` yourself at `WM_TIMER` handler and they just continue generating while the timer is active.
HardCoder1986
+2  A: 

Don't show a prompt - either log to a file/debug output, or just forcibly break the debugger (usually platform specific, eg. Microsoft's __debugbreak()). You have to do something more passive than show a dialog if there are threads involved which could fire lots of failures.

AshleysBrain
+3  A: 

It is important to understand how Windows UI programs work, to answer this question.

At the core of the Windows UI programming model is of course "the message" queue". Messages arrive in message queues and are retrieved using message pumps. A message pump is not special. It's merely a loop that retrieves one message at a time, blocking the thread if none are available.

Now why are you getting all these dialogs? Dialog boxes, including MessageBox also have a message pump. As such, they will retrieve messages from the message queue (It doesn't matter much who is pumping messages, in the Windows model). This allows paints, mouse movement and keyboard input to work. It will also trigger additional timers and therefore dialog boxes.

So, the canonical Windows approach is to handle each message whenever it arrives. They are a fact of life and you deal with them.

In your situation, I would consider a slight variation. You really want to save the state of your stack at the point where the assert happened. That's a particularity of asserts that deserves to be respected. Therefore, spin off a thread for your dialog, and create it without a parent HWND. This gives the dialog an isolated message queue, independent of the original window. Since there's also a new thread for it, you can suspend the original thread, the one where WM_TIMER arrives.

MSalters
Am I right, that suspending the main thread is not universal. I mean, like, I could have several main threads or something like that, or even use some 3rdparty threading lib, like `boost::thread`... Doest this mean I will have to write application-specific assert code?
HardCoder1986
There's actually no such thing as "the" main thread indeed. But that doesn't matter. You should block the thread that asserted, independ of which thread that was.
MSalters
+1  A: 

My own ASSERT implementation calls DebugBreak() or as alternative INT 3 (__asm int 3 in MS VC++). An ASSERT should break on the debugger.

Hernán
+1  A: 

Create a worker thread for your debugging code. When an assert happens, send a message to the worker thread. The worker thread would call SuspendThread on each thread in the process (except itself) to stop it, and then display a message box.

To get the threads in a process - create a dll and monitor the DllMain for Thread Attach (and Detach) - each call will be done in the context of a thread being created (or destroyed) so you can get the current thread id and create a handle to use with SuspendThread.

Or, the toolhelp debug api will help you find out the threads to pause.

The reason I prefer this approach is, I don't like asserts that cause side effects. Too often Ive had asserts fire from asynchronous socket processing - or window message - processing code - then the assert Message box is created on that thread which either causes the state of the thread to be corrupted by a totally unexpected re-entrancy point - MessageBox also discards any messages sent to the thread, so it messes up any worker threads using thread message queues to queue jobs.

Chris Becke
Do I have any possibility to also pause `not-winapi` threads, like threads created using `boost::thread` or something like that?
HardCoder1986
*all* threads (on Windows) are winapi threads: boost::thread, pthread-win32 and so on are wrappers around winapi threads on the Windows platform.
Chris Becke