Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6686975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:14:02+00:00 2026-05-26T05:14:02+00:00

So a bit of a weird bug going on when getting a WM_KEYDOWN msg

  • 0

So a bit of a weird bug going on when getting a WM_KEYDOWN msg in my Window class.

I have a Global WndProc function that determines which window instance it is and sends the message to it’s own local WndProc function.

//Every Windows Message will hit this function. 
LRESULT CALLBACK GlobalWndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {

    //Get the Window Instance from the userdata
    Window* targetWindow = (Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA);

    //If no window exists, then it must be the first time so we should set it
    if (!targetWindow) {
        //First let's try and extract the Window instance pointer from the lparam
        CREATESTRUCT* createStruct = (CREATESTRUCT*)lparam;
        if (createStruct) {
            targetWindow = (Window*)createStruct->lpCreateParams;
            SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)targetWindow);
        }
        else {
            //It was some other message which we just can't deal with right now
            return DefWindowProc(hwnd, msg, wparam, lparam);
        }
    }

    //Now we can pipe it to the Window's local wnd proc function
    return targetWindow->LocalWndProc(hwnd, msg, wparam, lparam);
}

And my local wndproc looks like this:

LRESULT CALLBACK Window::LocalWndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
    switch (msg) {

    case WM_KEYDOWN:
        ToggleFullScreen(!m_fullScreen);
        return 0;
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
        break;

    case WM_CLOSE:
        PostQuitMessage(0);
        return 0;
        break;

    default:
        return DefWindowProc(hwnd, msg, wparam, lparam);
    }
}

So it’s pretty simple at this point. If any key is pressed, the window should call its member function ToggleFullScreen.

Now for some reason when i run this and I hit any key on the keyboard, I get an exception fault:

Unhandled exception at 0x77c015ee in Athena_Debug.exe: 0xC0000005: Access violation reading location 0x0000012d.

With CallStack:

ntdll.dll!77c015ee()    
ntdll.dll!77bf015e()    
user32.dll!7588788a()   

Athena_Debug.exe!Athena::Window::HandleWindowsMessage() Line 195 + 0xc bytes C++
Athena_Debug.exe!Athena::AthenaCore::StartEngine() Line 96 + 0x12 bytes C++
Athena_Debug.exe!WinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, char * lpCmdLine, int nCmdShow) Line 44 C++
Athena_Debug.exe!__tmainCRTStartup() Line 547 + 0x2c bytes C
Athena_Debug.exe!WinMainCRTStartup() Line 371 C
kernel32.dll!7702339a()

The line it actually breaks on is the DispatchMessage(&msg) line located here:

MSG Window::HandleWindowsMessage() {
    MSG msg;
    ZeroMemory(&msg, sizeof(MSG));
    if (m_realTime) {
        PeekMessage(&msg, m_hwnd, 0, 0, PM_REMOVE);
    }
    else {
        GetMessage(&msg, m_hwnd, 0, 0);
    }

    if (msg.message != WM_NULL) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg;
}

So the interesting thing is that if I comment out ToggleFullScreen and instead put an OutputDebugTrace there it works just fine and outputs the debug trace. It appears like it can’t resolve the function for an instance? If I comment everything out in ToggleFullScreen, it still crashes. If i create a brand new function that does nothing and call it in response to WM_KEYDOWN it still errors.

Anyone have any idea why this is happening or some ideas on how I can go about fixing it?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:14:03+00:00Added an answer on May 26, 2026 at 5:14 am

    I would be guessing that you have wrong pointer obntained from GetWindowLongPtr, as it was not set by SetWindowLongPtr. As I wrote in my comment, you should not test if it’s empty (I have no idea what is there by default but i wouldn’t assume anything.), but set it when you recieve WM_CRERATE (which is guaranteed to be the first message sent to a window). Have you tested if SetWindowLongPtr is acctually called?

    Other messages are not dependent on any contents of Window class, so they are working well with wrong this pointer – it is an error to call method for wrong pointer, but if method is not actually using it, it will still work well.

    EDIT:

    Another possbile cause: Why are you not using result value from PeekMessage? i see that you are testing for WM_NULL, which should be 0 at this point (you are zeroing memory), but PeekMessage does not guarantee that it’s not touching MSG structure even if it’s not retrieving anything. Using PeekMessage result should be used at all times. Plus zeroing memory is rather inefficient at this point.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this weird bug, where C++ calls the wrong function: So this bit
all. I'm having a bit of weird problem with client server program. I have
Weird question, but one that puzzles me a bit. In the old days when
Bit support question. Apologies for that. I have an application linked with GNU readline.
I'm trying to use the .NET class OpenFileDialog in C++ and getting a weird
Maybe my question might seem a bit weird, but I have an activity in
I've got an expandable css menu that is acting a bit weird in ie,
The question is a little bit weird, but that's what I need ;) Usually,
OK so this might sound a bit weird! The thing is that in a
The title is a bit weird, so let me clarify. I have two objects,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.