#GeneralPurposeAllocator causing segfault in Raylib?

1 messages · Page 1 of 1 (latest)

wild raptor
#

I'm playing around with raylib and trying to use a gpa causes a segfault when calling in to the lib, but using a page_allocator seems to work fine. Anyone have any ideas, what am I missing? Docs say to use .init instead of default struct init, but I'm not sure how to. Using the gpa in this manner elsewhere works as expected.
zig version 14.something, raylib 5.5

const std = @import("std");
const rl = @cImport({
    @cInclude("raylib.h");
});

pub fn main() !void {
    rl.InitWindow(1920, 1080, "test");
    rl.SetTargetFPS(60);

    // this causes segfault in rl.DrawText()
    // var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    // defer _ = gpa.deinit();
    // const allocator = gpa.allocator();

    // this works fine
    const allocator = std.heap.page_allocator;

    while (!rl.WindowShouldClose()) {
        rl.BeginDrawing();
        rl.ClearBackground(rl.DARKGRAY);

        const fps = try std.fmt.allocPrint(allocator, "{d}", .{rl.GetFPS()});
        rl.DrawText(fps.ptr, 10, 10, 20, rl.LIGHTGRAY);

        rl.EndDrawing();
    }
    rl.CloseWindow();
}
humble wing
#

raylib is expecting a null terminated string but you're not giving it one, change allocPrint to allocPrintZ

wild raptor
#

ahk, so I'm getting lucky with the page

#

makes sense

humble wing
#

also you're leaking that memory, since the text is small with a known limit you just can write to an array with bufPrintZ to avoid any allocation