Archived

This topic is now archived and is closed to further replies.

Void

For those using Messageboxes when catching exceptions: Read

Recommended Posts

I bring to your attention when using messageboxs with exceptions If you use messagebox to display when an exception is thrown (typically in the main), the handle in the messagebox must not be NULL.. If it is NULL, it will crash if exception is thrown in the WM_MOUSEMOVE message, but not others like WM_KEYDOWN. There may be a few others but I''m not waiting to find out more.. If you are running fullscreen, you will just get an abnoral program termination messagebox and the system will crash.. Take heed..and obey.. or bang head against the wall like I did.. An example like this
    
try
{
	while(1)
	{
		// your message loop


	}
}
catch (...)
{
	// if you want to display a messagebox here, you better give a NON-NULL handle
	MessageBox(hwnd, "Exception thrown here", "Error", MB_OK);
}    

Share this post


Link to post
Share on other sites
Guest Anonymous Poster


FYI -

I frequently use MessageBox with a -0- window handle. I think your problem might lay elsewhere.

If you are throwing an exception in the WM_MOUSEMOVE handler, it might be that you are just running out of stack space as those WM_MOUSEMOVE messages can come pretty fast.

"If you are running fullscreen.." ...are you using DirectX and trying to display a message box? If so, are you calling FlipToGDISurface() before trying to display your message box?

Share this post


Link to post
Share on other sites
I never used the flip to GDI function. I have a global Window handle and use that with my messageBox functions.. it works great.

Share this post


Link to post
Share on other sites

It doesn''t have to be fullscreen. I test it on a windowed application and it still does that..

And as I said, it doesn''t happen in other messages, only the WM_MOUSEMOVE..

Here is my test program for you guys to try. Just create a empty win32 application.

Press E to throw an exception. Note is quit cleanly.

If you move the mouse for a while ( I use a static to keep track how many times the WM_MOUSEMOVE message is received, and throw an exception after receiving it for 10 times, then an exception is thrown. See how it crashes..


    
#include <windows.h>

//---------- Exception class


class CException
{
public:
CException(const char *string) { strncpy(m_error_string, string, 128); }

char *Get_String() { return(m_error_string); };

private:
char m_error_string[128];

}; // end class CException


//---------- Wndproc


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch(msg)
{
case WM_KEYDOWN:

switch(wparam)
{
case VK_ESCAPE: PostQuitMessage(0); break;



//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

// the exception thrown by keyboard is ok

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

case ''E'': throw CException("Thrown by key pressed"); break;
} // end switch(wparam)

return(0);

break;
case WM_MOUSEMOVE:

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

// This is the part where it dies

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


static count;

if (count++ > 10)
throw CException("Thrown by mouse pressed");


return(0);

break;
}

return(DefWindowProc( hwnd, msg, wparam, lparam ));

} // end wndproc


//---------- win main


int WINAPI WinMain(HINSTANCE h_instance, HINSTANCE h_previ_nstance, LPSTR lp_cmd_line, int show_cmd)
{

// create window

//----------------

WNDCLASSEX wndclass; // generic window class

HWND hwnd; // window handle


wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; //types of received message

wndclass.lpfnWndProc = (WNDPROC)WndProc;// event handler

wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = h_instance;
wndclass.hIcon = NULL;
wndclass.hCursor = NULL; // cursor

wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); // background

wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "Test";
wndclass.hIconSm = NULL; // small icon


if (!RegisterClassEx(&wndclass)) // registering class

return(0);

hwnd = CreateWindowEx( 0, "Test", "Test", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
0, 0, 640, 480,
NULL, NULL, h_instance, NULL);

if (!hwnd) // if error

return(0);

ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
SetFocus(hwnd);

// the main message pump

//----------------------------------------------------------------------


MSG msg;

try
{
while(1) // the main message pump

{
if (PeekMessage(&msg, NULL,0,0,PM_REMOVE))
{
if (WM_QUIT == msg.message)
break;

TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
WaitMessage();
} // end while(1)

} // end try

catch(...)
{
// display the exception

//----------------------

MessageBox(NULL, "exception", "Error", MB_OK | MB_TOPMOST);
}

return(0);
} // end winmain

Share this post


Link to post
Share on other sites