[英]Process returned -1073741819 (0xC0000005) in code::blocks c++
[英]Process finished with exit code -1073741819 (0xC0000005) C++ clion
我正在关注本教程,但是当我在时间戳运行时出现错误:
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。 过了一会儿,我让它再次工作,我的调试器现在可以工作了,但也许它仍然与那个错误有关。
在接收到WM_SIZE
消息之前, buffer_height
和buffer_width
都是0
,因此pixel
循环在实际创建 bitmap 之前不会尝试访问buffer_memory
。 但是,您没有验证VirtualAlloc()
是否成功。
0xC0000005
是访问冲突。 您在某处访问无效的 memory。 您需要调试代码才能找到它。
此外,您正在从消息循环内部绘制 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.