This is quite bizarre.
My (total) guess is that the MessageBox is failing the first time the errror is triggered. If the exit call is present that's it. If the exit call is not present the error is retriggered and the message box comes up on the second attempt.
If that is the case, the question remains why MessageBox is failing.
I hope we get an answer for this thread as it is rather bewildering.
Calling MessageBox in WM_PAINT may mean you are running out of gdi object as assumably each MessageBox would trigger more WM_PAINT messages and so on. Maybe it could also be a stack overflow as each message box would trigger another.