#Issue with WebAssembly and page_allocator

1 messages · Page 1 of 1 (latest)

scenic panther
#

Hi,
I really have troubles with allocators when running my code as a Webassembly program. So to understand I created a little PoC to try to reproduce the problems on a smaller scale.

I took the simple WASM canvas example from Daneelsan (https://github.com/daneelsan/minimal-zig-wasm-canvas) and the zig-string (https://github.com/JakubSzark/zig-string) library to test.

I "only" added to te WASM canvas example the following code:

export fn stringCheck() void {
    const alloc: std.mem.Allocator = std.heap.page_allocator;

    // create strings
    Console.log("Allocating string for scroller text", .{});
    var text_str: String = String.init(alloc);
    defer text_str.deinit();

    if (text_str.concat("Hellllloooo where are the cool kids ??????????????")) |_| {
        Console.log("String created: {s}", .{text_str.str()});
    } else |err| {
        Console.log("failed: {s}", .{@errorName(err)});
    }
}

This part "works" but breaks the canvas part:

Uncaught (in promise) TypeError: Cannot perform %TypedArray%.prototype.slice on a detached ArrayBuffer
    at Uint8Array.slice (<anonymous>)
    at drawCheckerboard (script.js:60:48)
    at script.js:72:5

Commenting the call to stringCheck solves the problem.

full source here: https://github.com/matt-grain/minimal-zig-wasm-canvas

Any idea what is happening?

Thanks!

hard nymph
#

When you grow the memory, any previous buffers become invalid. drawCheckerBoard is called while using wasmMemoryArray which hs been invalidated by the grown memory.

#

Within that function you probably want to reread the memory.buffer