Archived

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

smitty1276

Win32 wrapper STACK OVERFLOW!! Why?!?

Recommended Posts

OK... I stole virtually every idea for a window wrapper class from http://www.gamedev.net/reference/articles/article1810.asp... I'm sure most of you have seen that particular article. So I started slowly coding it, one small step at a time (I figured I would understand what was going on better if I did NOT copy/paste, etc)... I have very little here, but I thought once I got it working on the most basic level, I would customize it the way I like it and get it really working... all it does is open a window, send messages to the MsgRouter static function, and call the default window procedure. I haven't even implemented the classes own handler yet... that's how little I have. Anyway, it runs, I can see the Window flash on the screen, I can trace long enough to see that the MsgRouter succeeds in extracting the address of the instance from the lParam on the create event, etc., then I can trace as it cycles through the message loop over and over... so I let it run. I get a STACK OVERFLOW error everytime. Here is my source code for the class... it's very small so it should be easy to read...
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

class CGLWindow
{
private:
	//*** Window handle and app instance

	HWND		m_hWnd;
	HINSTANCE	m_hInstance;
	
	//*** The primary static callback function for messages

	//*** Will route messages to specific instances

	static LRESULT CALLBACK MsgRouter(HWND hwnd,     UINT message,
                                          WPARAM wparam, LPARAM lparam);

public:
	//*** Constructor ***

	CGLWindow( HINSTANCE hInstance );

	bool Create			( DWORD dwWindowStyle );
	bool Show			( int nCmdShow );
	bool HandleMessages	        ( void );

};
#endif //_GLWINDOW_H_


//***** CONSTRUCTOR *****

CGLWindow::CGLWindow( HINSTANCE hInstance )
{
	m_hWnd = NULL;
	m_hInstance = hInstance;
}

//***** ROUTES MESSAGES TO SPECIFIC INSTANCES MEMBER FUNC MSG HANDLER *****

LRESULT CALLBACK CGLWindow::MsgRouter(HWND hwnd,     UINT message,
                                      WPARAM wparam, LPARAM lparam)
{	
	CGLWindow *pWnd = NULL;
	
	if(message == WM_NCCREATE)
	{
		// retrieve Window instance from window creation data and associate

		pWnd = reinterpret_cast<CGLWindow *> (((LPCREATESTRUCT)lparam)->lpCreateParams);
		SetWindowLong(hwnd, GWL_USERDATA, reinterpret_cast<long>(pWnd));
	}
	else
	{
		// retrieve associated Window instance

		pWnd = reinterpret_cast<CGLWindow *> (GetWindowLong(hwnd, GWL_USERDATA));
	}

	// call the windows message handler

	//pWnd->WndProc(message, wparam, lparam);


	return DefWindowProc(hwnd, message, wparam, lparam);
}

bool CGLWindow::Create(DWORD dwWindowStyle )
{

	WNDCLASSEX	wc;
	wc.cbSize	= sizeof(WNDCLASSEX);
	wc.style	= CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc	= CGLWindow::MsgRouter;
	wc.cbClsExtra	= 0;
	wc.cbWndExtra	= 0;
	wc.hInstance	= m_hInstance;
	wc.hIcon	= LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor	= LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground= (HBRUSH) GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName	= NULL;
	wc.lpszClassName= "GLWindow";
	wc.hIconSm	= LoadIcon(NULL, IDI_WINLOGO);
	if( !RegisterClassEx(&wc) )
		return false;

	//TO DO: Lose the dummy args here

	m_hWnd = CreateWindowEx(NULL, "GLWindow", "GL Test App", 
				dwWindowStyle,
				200, 200, 600, 600, NULL, NULL, m_hInstance, this);

	if(!m_hWnd)
		return false;

	return true;
}

bool CGLWindow::Show( int nCmdShow )
{
	return ShowWindow(m_hWnd, nCmdShow);
}

bool CGLWindow::HandleMessages( void )
{
  static MSG msg;

  if(!m_hWnd)
    return false;

  if( PeekMessage(&msg, m_hWnd, 0, 0, PM_REMOVE) )
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  else
  {
    return false;
  }

  return true;
}
And here is my WinMain...
   
#include <windows.h>
#include "glWindow.h"

INT APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
		     LPSTR     lpCmdLine, int       nShowCmd)
{
	CGLWindow *pWnd = new CGLWindow(hInstance);

	pWnd->Create(WS_OVERLAPPEDWINDOW | WS_VISIBLE);
	pWnd->Show(SW_SHOW);
	while( pWnd->HandleMessages() )
		; //Message loop


	return 0;
}
That is ABSOLUTELY ALL I HAVE... anyone see where I could be generating a stack overflow error? [edited by - smitty1276 on January 16, 2004 2:56:03 AM] [edited by - smitty1276 on January 16, 2004 2:57:29 AM]

Share this post


Link to post
Share on other sites
It seems to be working here now too... I''m baffled.

I''ll keep my fingers crossed... I do have another question though...

Just as a test, I inserted this line after I had retrieved the pointer to the class instance in the MsgRouter function...

SetWindowText(pWnd->GetHWND(), "Everything seems to work!");

(BTW, I added the GetHWND() method to return m_hWnd...)

It does NOT like that SetWindowText call... Unhandled exception Access violation C0000005 etc. etc....

What NOW!!! Any ideas why it doesn''t like that? GetHWND is a public function that simply returns the private member variable m_hWnd, which you can see in my original post above.

Share this post


Link to post
Share on other sites