SimonLSchlee / zig15game

A 15 puzzle game implemented with raylib.
https://simonlschlee.github.io/zig15game/
MIT License
5 stars 0 forks source link

possibly a problem with allocators #1

Open oshyshko opened 6 days ago

oshyshko commented 6 days ago

I am getting error.OutOfMemory when using any allocators with your setup.

error.OutOfMemory

Please, confirm if you also have this problem, and if you have a workaround.

Here's a minimal main.zig:

const std = @import("std");

pub fn main() !void {
    const allocator = std.heap.wasm_allocator;
    _ = try allocator.create(i32);
}

or

const std = @import("std");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = gpa.allocator();
    _ = try allocator.create(i32);
}

Environment:

$ ./emsdk list

(edited)

The *recommended* precompiled SDK download is 3.1.66 (243eae09cf5c20c4fde51a620b92f483255c8214).

This is equivalent to installing/activating:
         3.1.66             INSTALLED

All recent (non-legacy) installable versions are:
         3.1.66    INSTALLED

The additional following precompiled SDKs are also available for download:
    *    sdk-releases-243eae09cf5c20c4fde51a620b92f483255c8214-64bit    INSTALLED

The following precompiled tool packages are available for download:
    (*)    releases-243eae09cf5c20c4fde51a620b92f483255c8214-64bit  INSTALLED
    (*)    node-18.20.3-64bit           INSTALLED
     *     python-3.9.2-64bit           INSTALLED

$ zig version
0.14.0-dev.1399+a76e98e7d
SimonLSchlee commented 6 days ago

Here is an example branch https://github.com/SimonLSchlee/zig15game/tree/wasm-allocator-test commit https://github.com/SimonLSchlee/zig15game/commit/1716c998db0a118c832da17e1d8eb86b47b432b2 for how you can get allocators to work, I am not sure if this is the best or only way.

Once you have built the wasm application you need to host it with the headers seen in the server.go help script (run via go run server.go see the commit above), this is a convenience script for running a webserver with the right headers for debugging locally from this example https://github.com/permutationlock/zig_emscripten_threads

You then should see the INFO logs for the number if you visit localhost:8083 and open your console.

oshyshko commented 6 days ago

Looks like there is a problem with emscripten https://github.com/ziglang/zig/issues/19072

Switching std.heap.c_allocator made it work.

Applying changes from 1716c99 didn't work. The error.OutOfMemory error changed to `RuntimeError: index out of bounds.

image
SimonLSchlee commented 6 days ago

I think you are running different code, because compiling commit 1716c99 with zig 0.13.0 works for me, so I think there may be another issue, either that or the error appears when making more / bigger allocations.

But if using std.heap.c_allocator works, I think that is good enough as a work around until zig or emscripten fixes something?