You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Raylib在Visual Studio绘制自定义字体时遇0xc0000005错误求助

解决Raylib加载自定义字体导致的0xc0000005错误

错误码0xc0000005是内存访问违规,问题根源很明确:你在InitWindow()之前调用了LoadFont()。Raylib的资源加载(包括字体)依赖OpenGL上下文,而这个上下文只有调用InitWindow()后才会创建,提前加载字体会导致程序访问无效内存,直接触发崩溃。

修正后的代码

#include <raylib.h>

int main(int argc, char* argv[]) {
    const int screenWidth = 1080;
    const int screenHeight = 768;
    
    // 先初始化窗口,创建必要的OpenGL上下文
    InitWindow(screenWidth, screenHeight, "test");
    // 上下文就绪后再加载字体
    Font font = LoadFont("assets/font.ttf");

    SetTargetFPS(60);

    Vector2 vector{ 20.00f, 30.00f };
    while (!WindowShouldClose()) {
        BeginDrawing();
            ClearBackground(WHITE);
            DrawTextEx(font, "Hello World!", vector, 20.0, 10, BLACK);
        EndDrawing();
    }

    UnloadFont(font);
    CloseWindow();

    return 0;
}

额外注意事项

  • 确认assets/font.ttf的路径正确,程序运行时能找到该文件(可将字体放到输出目录的assets文件夹,或用绝对路径测试)。
  • Raylib所有资源加载操作(纹理、字体、声音等)都必须放在InitWindow()之后执行,资源卸载则要在CloseWindow()之前完成。

内容的提问来源于stack exchange,提问作者telamont12

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 14:12:02