进程结束,退出代码为 -1073741819 (0xC0000005) C++ clion

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

我正在关注这个 tutorial 但是当我 运行 在时间戳时我收到错误:

Process finished with exit code -1073741819 (0xC0000005)

我的密码是

#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);

    }



}

不过好像错误是

                *pixel++ = 0xff500;

一旦我把它取出来,我就不会再收到错误了。 我正在调查这个,但大多数错误来自 python/pycharm 而不是 clion。

此外,在我这样做之前,我正在做一个遗传算法(整洁)并下载 boost 以进行序列化。但它不起作用,并导致我的调试器出现很多错误,提示“python 脚本停止工作。”

长话短说,我卸载了我的 mingw 和 clion。过了一会儿,我让它再次工作,我的调试器现在可以工作了,但也许它仍然与那个错误有关。

buffer_heightbuffer_width 都是 0 直到收到 WM_SIZE 消息,所以 pixel 循环不会尝试访问 buffer_memory 直到实际创建位图。但是,您没有验证 VirtualAlloc() 是否成功。

0xC0000005 是访问冲突。您正在某处访问无效内存。您需要调试代码才能找到它。

此外,您正在从消息循环内部利用 window。您应该只从 WM_PAINT 消息处理程序内部使用它。

试试这个:

#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;
}