Get Unhandled exception at 0x00f85069
my program breaks and says this
Unhandled exception at 0x00f85069 in Monopoly.exe: 0xC0000005: Access violation writing location 0x0000000c.
i made a win32 wrapper. i h开发者_开发技巧ave two WndProc one is static and the other one not. the static WndProc calls the nonstatic WndProc. when i try to get messages it works fine but when i try to set a value for something it throw a exception.
here is my code for two WndProc(the first one its that static)LRESULT CALLBACK Window::StaticWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
if ( msg == WM_CREATE )
{
SetWindowLongPtr( hWnd, GWLP_USERDATA, (LONG)((CREATESTRUCT *)lParam)->lpCreateParams );
}
Window *targetApp = (Window*)GetWindowLongPtr( hWnd, GWLP_USERDATA );
return targetApp->WndProc( hWnd, msg, wParam, lParam );
}
LRESULT CALLBACK Window::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
z=-14; //IT THROW EXCEPTION
break;
case WM_RBUTTONDOWN:
z-=1;
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Thanks in Advance
EDIT:
VARABILES int z;How are you creating your window? Did you pass a valid pointer to an instance of Window
to your CreateWindow()
or CreateWindowEx()
function via the lpParam
parameter? For example, if your window wrapper has a Create()
function or something like that:
void Window::Create()
{
/* ... */
// Pass a pointer to ourselves to CreateWindow() via the lpParam parameter.
// CreateWindow() then passes that pointer to your window procedure
// (StaticWndProc) via WM_CREATE and WM_NCCREATE in the lpCreateParams member
// of CREATESTRUCT. This way the window procedure will know which instance to
// call WndProc() on.
CreateWindow(lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight,
hWndParent, hMenu, hInstance, (LPVOID)this);
/* ... */
}
The object on which you're calling WndProc
seems to be non-existent (null?) and you have an access violation upon trying to write the memory in z -= 14
which is this->z -= 14
(this
being an invalid pointer). That's my guess.
Also, Access Violation is not not an exception in C++ terms. :)
Well, an access violation (0xC0000005
) means you accessed memory that you shouldn't have. In this case it says you were trying to write to 0x0000000c
. Since you (your debugger?) say that the assignment to z
causes it, may we see the definition of that symbol, please? Also, is it really z = -14
or z -= 14
?
Edit: I think you need to replace ((CREATESTRUCT *)lParam)->lpCreateParams
with a valid pointer to an instance of class Window
.
Edit #2: What happens is this: On WM_CREATE
, you set GWLP_USERDATA
to a value that happens to be equal to NULL
. Subsequently, you read that value and treat it as a valid pointer to a Window
by invoking a non-static member function on that pointer. Class member functions are implemented by the compiler a lot like so
LRESULT CALLBACK <mangled_name ("Window::WndProc")> (Window * const this, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
// ...
}
That is the reason that you can actually invoke a member function on a NULL
pointer. However, once you access a member variable, like z
, this breaks. The compiler inserted code similar to this *((int*) (this + 0xc)) = -14
, (which BTW means that z
lies 0xc bytes into your Window
instance), which, since this == NULL
, broke.
精彩评论