Search code examples
c++clionexit-code

Process finished with exit code -1073741819 (0xC0000005) C++ clion


I'm following this tutorial but when i run at the time stamp I get the error:

Process finished with exit code -1073741819 (0xC0000005)

my code is

#include <windows.h>
bool running = true;


void* buffer_memory;
int buffer_width;
int buffer_height;

BITMAPINFO buffer_bit_map_info;
LRESULT CALLBACK window_callback(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    LRESULT result = 0;

    switch (uMsg) {
        case WM_CLOSE:
        case WM_DESTROY: {
            running = false;
        } break;

        case WM_SIZE: {
            RECT rect;
            GetClientRect(hwnd, &rect);
            buffer_width = rect.right - rect.left;
            buffer_height = rect.bottom - rect.top;

            int buffer_size = buffer_height* buffer_height* sizeof(unsigned  int);


            if (buffer_memory) VirtualFree(buffer_memory, 0, MEM_RELEASE);
            buffer_memory = VirtualAlloc(0, buffer_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);

            buffer_bit_map_info.bmiHeader.biSize = sizeof(buffer_bit_map_info.bmiHeader);
            buffer_bit_map_info.bmiHeader.biWidth = buffer_width;
            buffer_bit_map_info.bmiHeader.biHeight = buffer_height;
            buffer_bit_map_info.bmiHeader.biPlanes = 1;
            buffer_bit_map_info.bmiHeader.biBitCount = 32;
            buffer_bit_map_info.bmiHeader.biCompression = BI_RGB;

        } break;

        default: {
            result = DefWindowProc(hwnd, uMsg, wParam, lParam);
        }
    }
    return result;
}

int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {

    // Create Window Class
    WNDCLASS window_class = {};
    window_class.style = CS_HREDRAW | CS_VREDRAW;
    window_class.lpszClassName = "Game Window Class";
    window_class.lpfnWndProc = window_callback;

    // Register Class
    RegisterClass(&window_class);

    // Create Window
    HWND window = CreateWindow(window_class.lpszClassName, "Pong - Tutorial", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 1280, 720, 0, 0, hInstance, 0);


    HDC hdc = GetDC(window);

    while (running) {
        // Input
        MSG message;

        while (PeekMessage(&message, window, 0, 0 ,PM_REMOVE)){
            TranslateMessage(&message);
            DispatchMessage(&message);
        }
        //simulate
        unsigned int* pixel = (unsigned int*)buffer_memory;
        for (int y = 0; y < buffer_height; y++){
            for (int x = 0; x < buffer_width; x++){
                *pixel++ = 0xff500;
            }
        }

        //Render
        StretchDIBits(hdc, 0,0, buffer_width, buffer_height, 0, 0, buffer_width, buffer_height, buffer_memory, &buffer_bit_map_info, DIB_RGB_COLORS, SRCCOPY);

    }



}

But it seems the error is

                *pixel++ = 0xff500;

Once I take this out it I dont get the error anymore. I was looking into this but most of these errors are from python/pycharm not clion.

Also, before I did this I was doing a genetic algorithm(neat) and downloaded boost for seralization. But it didn't work and caused a lot of errors for my debugger saying "python script stopped working."

So long story short I uninstalled my mingw and clion. After a while I got it working again and my debugger works now but maybe it still has something to do with that error.


Solution

  • buffer_height and buffer_width are both 0 until a WM_SIZE message is received, so the pixel loop won't try to access buffer_memory until a bitmap is actually created. However, you are not validating that VirtualAlloc() is successful.

    0xC0000005 is an Access Violation. Somewhere you are accessing invalid memory. You need to debug your code to find it.

    Also, you are drawing on the window from inside your message loop. You should be drawing on it only from inside of a WM_PAINT message handler instead.

    Try this instead:

    #include <windows.h>
    
    void* buffer_memory = NULL;
    int buffer_width = 0;
    int buffer_height = 0;
    BITMAPINFO buffer_bit_map_info;
    
    LRESULT CALLBACK window_callback(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
        switch (uMsg) {
            case WM_CLOSE: {
                DestroyWindow(hwnd);
                return 0;
            }
    
            case WM_DESTROY: {
                if (buffer_memory) VirtualFree(buffer_memory, 0, MEM_RELEASE);
                buffer_memory = NULL;
                buffer_width = buffer_height = 0;
                PostQuitMessage(0);
                return 0;
            }
    
            case WM_SIZE: {
                RECT rect;
                GetClientRect(hwnd, &rect);
    
                buffer_width = rect.right - rect.left;
                buffer_height = rect.bottom - rect.top;
    
                int buffer_size = buffer_height * buffer_height * sizeof(unsigned int);
    
                if (buffer_memory) VirtualFree(buffer_memory, 0, MEM_RELEASE);
                buffer_memory = VirtualAlloc(0, buffer_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
    
                buffer_bit_map_info.bmiHeader.biSize = sizeof(buffer_bit_map_info.bmiHeader);
                buffer_bit_map_info.bmiHeader.biWidth = buffer_width;
                buffer_bit_map_info.bmiHeader.biHeight = buffer_height;
                buffer_bit_map_info.bmiHeader.biPlanes = 1;
                buffer_bit_map_info.bmiHeader.biBitCount = 32;
                buffer_bit_map_info.bmiHeader.biCompression = BI_RGB;
    
                if (buffer_memory) {
                    unsigned int* pixel = (unsigned int*) buffer_memory;
                    for (int y = 0; y < buffer_height; ++y){
                        for (int x = 0; x < buffer_width; ++x){
                            *pixel++ = 0xff500;
                        }
                    }
                }
    
                InvalidateRect(hwnd, NULL, TRUE);
                break;
            }
    
            case WM_PAINT: {
                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hwnd, &ps);
    
                //Render
                if (buffer_memory) {
                    StretchDIBits(hdc, 0, 0, buffer_width, buffer_height, 0, 0, buffer_width, buffer_height, buffer_memory, &buffer_bit_map_info, DIB_RGB_COLORS, SRCCOPY);
                }
    
                EndPaint(hwnd, &ps);
                return 0;
            }
        }
    
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    
    int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
    
        // Create Window Class
        WNDCLASS window_class = {};
        window_class.style = CS_HREDRAW | CS_VREDRAW;
        window_class.lpszClassName = "Game Window Class";
        window_class.lpfnWndProc = window_callback;
    
        // Register Class
        RegisterClass(&window_class);
    
        // Create Window
        HWND window = CreateWindow(window_class.lpszClassName, "Pong - Tutorial", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 1280, 720, 0, 0, hInstance, 0);
    
        // Input
        MSG message;
    
        while (GetMessage(&message, NULL, 0, 0)) {
            TranslateMessage(&message);
            DispatchMessage(&message);
        }
    
        return 0;
    }