Jump to content
  • Advertisement
Sign in to follow this  
stenny

Unhandled Exception at return?

This topic is 3623 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey there! When I build a release version of my app, I get an unhandled exception in this piece of code (WinAPI setup):
HRESULT NSystem::Init(std::string strClass, std::string strIconFilename)
{
	// get the Instancehandle
	m_hInst	= GetModuleHandle(0);
	if(!m_hInst) { return E_FAIL; }

	// setup the WNDCLASSEX structure
	m_WndClass.lpszClassName	= strClass.c_str();
	m_WndClass.cbSize		= sizeof(WNDCLASSEX);
	m_WndClass.style		= CS_CLASSDC;
	m_WndClass.hInstance		= m_hInst;
	m_WndClass.lpfnWndProc		= (WNDPROC)WndProc;
	m_WndClass.hIconSm		= NULL;
	m_WndClass.hCursor		= LoadCursor(NULL, IDC_ARROW);
	m_WndClass.hbrBackground	= (HBRUSH)(COLOR_WINDOW +1);
	m_WndClass.cbClsExtra		= 0;
	m_WndClass.cbWndExtra		= 0;
	m_WndClass.lpszMenuName		= 0;

	// the icon
	if(strcmp(strIconFilename.c_str(), ""))
	{
		m_WndClass.hIcon		= LoadIcon(0, IDI_APPLICATION);
	} else {
		m_WndClass.hIcon		= HICON(LoadImage(NULL, strIconFilename.c_str(), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CXSMICON), LR_LOADFROMFILE));
	}

	// register the class
	RegisterClassEx(&m_WndClass);

	// store the classname
	m_strClass	= strClass;

	return S_OK; // THIS LINE IS WHERE VISUAL STUDIO POINTS TO //
}



When I start with debugging, I get an unhandled exception error at the very last line (return S_OK). Unhandled exception at 0x10232008 in UseNGraphics.exe: 0xC0000005: Access violation reading location 0x63696870. Anyone care to give a shot what the problem could be? -Stijn

Share this post


Link to post
Share on other sites
Advertisement
There's two things that jump out. Firstly, this is screaming at me:
m_WndClass.lpfnWndProc = (WNDPROC)WndProc;
Never ever, ever, ever cast function pointers like that. if it doesn't work without the cast, then there's something wrong with it and it needs fixed. If you need the cast, that means the function isn't in the correct format, and you're likely to get "weird stuff" happening, including crashes.

Secondly, the address 0x10232008 is near the base address for a custom DLL. Is this code in a DLL? If so, what is the m_hInst variable? Win32 expects the HINSTANCE parameter passed to the WNDCLASSEX struct to point to the HINSTANCE of the module containing the window proc; which would be your DLL in this case. Note that calling GetModuleHandle(NULL) from a DLL gives you the HINSTANCE of the EXE, not the DLL. If you want the HINSTANCE of the DLL, you need to grab it from DllMain() (A HMODULE is the same as a HINSTANCE here, and is safe to cast).

Share this post


Link to post
Share on other sites
you cant use std::string for your class name since strClass.c_str() returns a const referance to the string which gets destroyed when you returns.

take a const char* as the class name or just call it

m_WndClass.lpszClassName = "NSystem:Window"; or whatever

Share this post


Link to post
Share on other sites
You might want to start with checking the return value of RegisterClassEx for errors.

Second, are you sure (HBRUSH)(COLOR_WINDOW +1) is a valid brush?

Third, in C++ the line

if(strcmp(strIconFilename.c_str(), ""))


can (should?) be replaced by

if(strIconFilename.size() > 0)




Actually, your logic appears back to front here. strcmp will return 0, e.i. false, if the two strings are equal.

Share this post


Link to post
Share on other sites
Quote:
Original post by Molle85
you cant use std::string for your class name since strClass.c_str() returns a const referance to the string which gets destroyed when you returns.

take a const char* as the class name or just call it

m_WndClass.lpszClassName = "NSystem:Window"; or whatever
True, although this shouldn't affect this function; only if m_WndClass is used in another function. It's still a bug though.

Quote:
Original post by Promethium
Second, are you sure (HBRUSH)(COLOR_WINDOW +1) is a valid brush?
It is; you're allowed to take a COLOR_* constant and add 1 to it, then cast it to a brush to use that as a window background colour. I'm not sure if it's legal anywhere else though.[/quote]

Share this post


Link to post
Share on other sites
@Evil Steve
And here again, I have proof that I stand yet at the beginning of the programmersladder ;). I'm indeed looking for the handle of the exe though. I don't see why there's a problem with a DLL, since this code is in a lib (and there's no DLL having to do with it).

@Molle85
True. Good point....changed it ^^.

@Promethium
Hmm...that (HBRUSH)(COLOR_WINDOW +1) is probably a leftover of trying some things out. These things get filtered out when revising things over, but an unhandled exception's higher on the priority list ;)

I'll report back after these changes ;)

[EDIT]
And that did the trick. Removed the (WNDPROC) (There really should be a universal library of bad c++ design..thingies), and made the classname and caption const char*'s.

Thanks for helping out!
-Stijn

Share this post


Link to post
Share on other sites
Quote:
Original post by stenny
@Evil Steve
And here again, I have proof that I stand yet at the beginning of the programmersladder ;). I'm indeed looking for the handle of the exe though. I don't see why there's a problem with a DLL, since this code is in a lib (and there's no DLL having to do with it).
Ah, I was just guessing, based on the exception address [smile]

Quote:
Original post by stenny
@Promethium
Hmm...that (HBRUSH)(COLOR_WINDOW +1) is probably a leftover of trying some things out. These things get filtered out when revising things over, but an unhandled exception's higher on the priority list ;)
It's perfectly valid to do that. Note that if you're using this window for rendering (with OpenGL or D3D), you probably want to use NULL here, since Windows won't be repainting the window, D3D/OpenGL will.

Share this post


Link to post
Share on other sites
Quote:
It's perfectly valid to do that. Note that if you're using this window for rendering (with OpenGL or D3D), you probably want to use NULL here, since Windows won't be repainting the window, D3D/OpenGL will.

One thing's very clear. You know your stuff ;). Although lurking these forums made that already pretty much clear. [grin]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!