Unhandled Exception at return?

Started by
6 comments, last by stenny 15 years, 9 months ago
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
What do I expect? A young man's quest to defeat an evil sorceror while discovering the truth of his origins. A plucky youngster attended by her brutish guardian. A powerful artifact which has been broken into a small number of artifactlets distributed around the world.What do I want? Fewer damn cliches. - Sneftel
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).
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
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.
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.

@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
What do I expect? A young man's quest to defeat an evil sorceror while discovering the truth of his origins. A plucky youngster attended by her brutish guardian. A powerful artifact which has been broken into a small number of artifactlets distributed around the world.What do I want? Fewer damn cliches. - Sneftel
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.
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]

What do I expect? A young man's quest to defeat an evil sorceror while discovering the truth of his origins. A plucky youngster attended by her brutish guardian. A powerful artifact which has been broken into a small number of artifactlets distributed around the world.What do I want? Fewer damn cliches. - Sneftel

This topic is closed to new replies.

Advertisement