【问题标题】:Process finished with exit code -1073741819 (0xC0000005) C++ clion进程以退出代码 -1073741819 (0xC0000005) C++ clion 完成
【发布时间】:2021-03-25 21:15:28
【问题描述】:

我正在关注这个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。

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

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

【问题讨论】:

  • 您只在大小事件上分配缓冲内存。尝试在模拟/渲染部分周围添加if(buffer_memory){...}
  • @MikeVine `if (buffer_memory){ unsigned int* pixel = (unsigned int*)buffer_memory; for (int y = 0; y
  • 添加if (buffer_memory) 可能无关紧要,因为buffer_heightbuffer_width 都是0,直到收到WM_SIZE,所以pixel 循环将无法访问buffer_memory 直到实际创建位图。但是,您并未验证 VirtualAlloc() 是否成功。 0xC0000005 是访问冲突。您正在访问无效内存的地方。你需要调试你的代码才能找到它。

标签: c++ clion exit-code


【解决方案1】:

buffer_heightbuffer_width 在收到WM_SIZE 消息之前都是0,因此pixel 循环在实际创建位图之前不会尝试访问buffer_memory。但是,您并未验证 VirtualAlloc() 是否成功。

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

此外,您正在从消息循环内部的窗口上绘图。您应该仅从 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-10
    • 2020-06-13
    • 2023-04-06
    • 2019-01-05
    • 2020-03-19
    • 1970-01-01
    相关资源
    最近更新 更多