首页 > 解决方案 > 从 Visual Studio 运行时,基本 C++ SDL 程序抛出异常 0xC0000005

问题描述

使用 SDL 2.0.14,我编写了一个基本的 main 函数,创建一个空窗口并在 SDL_QUIT 事件时关闭它。从我正在使用的 Visual Studio 2019 IDE(x64 平台,调试和发布模式)运行时,程序在窗口关闭时运行良好;然后 IDE 生成以下消息:

Exception thrown at 0x00007FFBFAE55BB6 (ntdll.dll) in my_program.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFBEA2000F.

它还指向 exe_common.inl 文件中的代码。编译后的可执行文件运行时没有任何明显的问题。

我的程序的单一源文件包含以下内容:

#include <iostream>
#include <SDL.h>

int main(int argc, char* args[]){
    const int window_width{ 800 };
    const int window_height{ 600 };
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        std::cout << "SDL init failed" << std::endl;
        return 1;
    }
    SDL_Window* window = SDL_CreateWindow(
        "My Program",
        SDL_WINDOWPOS_UNDEFINED,
        SDL_WINDOWPOS_UNDEFINED,
        window_width,
        window_height,
        SDL_WINDOW_SHOWN);

    if (!window) {
        SDL_Quit();
        std::cout << "Could not create window: " << SDL_GetError() << std::endl;
        return 2;
    }

    bool quit = false;
    SDL_Event event;
    while (!quit) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                quit = true;
            }
        }
    }

    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

我的调用堆栈窗口的屏幕截图

标签: c++sdl-2

解决方案


推荐阅读