0

I have compiled the C++/Raylib game to the web, but after trying to run it I have seen these errors in console:

Uncaught RuntimeError: memory access out of bounds
at game.wasm:0x47615
at game.wasm:0x375b4
at game.wasm:0x37440
at browserIterationFunc (game.js:6921:63)
at callUserCallback (game.js:6150:9)
at Object.runIter (game.js:6205:11)
at Browser_mainLoop_runner (game.js:6891:26)

Then I switched to game.wasm:0x47615, and it contained this statement:

i32.load

Here is my build command:

em++ -o webTarget/game.html libraylib.a -O0 -Wall -s ASSERTIONS -s USE_GLFW=3 -DPLATFORM_WEB -s ALLOW_MEMORY_GROWTH=1 --preload-file textures main.cpp Entities.cpp Photos.cpp Game.cpp World.cpp EventsHandler.cpp Entity.cpp

I am really sorry that I can not give minimal reproducible examble, because I do not know where to start searching. But here it is a file where I am invoking main loop:

#include "Game.h"

#include <functional>

#include "options.h"
#include "photos_data.h"

#ifdef __EMSCRIPTEN__
#include "emscripten.h"
#endif

Game::Game(const std::string_view& windowTitle, int level)
{
    this->init(windowTitle, level);
}

void Game::init(const std::string_view& windowTitle, int level)
{
    InitWindow(Options::WindowSize.x, Options::WindowSize.y, windowTitle.data());

    world = std::make_unique<World>(LevelsPhotos[level], &m_eventsHandler, Options::ViewportSize, Options::UpdateRectSize, Options::WindowSize, Options::FramesPerMove, Options::MaxPlayerShift);

#ifdef __EMSCRIPTEN__
    emscripten_set_main_loop_arg(MainloopCallback, (void*)this, 0, true);
#else
    SetTargetFPS(Options::FPS);

    while (!WindowShouldClose())
    {
        this->mainloop();
    }
#endif

    CloseWindow();
}

void Game::mainloop()
{
    m_eventsHandler.handleEvents();

    if (world->currentFrame == 0)
    {
        world->update();
    }

    BeginDrawing();
    ClearBackground(BLACK);
    world->draw();
    EndDrawing();
}

#ifdef __EMSCRIPTEN__
void MainloopCallback(void* arg)
{
    static_cast<Game*>(arg)->mainloop();
}
#endif

Also I can give a link to the source code in GitHub: link

Edit: this code could be compiled well for x64 or x86 platforms, and the only differences are in the parts #ifdef, which are only in Game.cpp file (given code). So the error must be only in these parts of code, and I think that somewhere in emscripten_set_main_loop_arg

Edit: after adding a flag -s MAXIMUM_MEMORY=4gb, it is showing another error in browser console

Uncaught RuntimeError: table index is out of bounds
    at game.wasm:0x5667a
    at game.wasm:0x4233d
    at game.wasm:0x4211f
    at browserIterationFunc (game.js:6525:58)
    at callUserCallback (game.js:5849:3)
    at Object.runIter (game.js:5912:4)
    at Browser_mainLoop_runner (game.js:6509:20)
Roman Leshchuk
  • 192
  • 1
  • 7

0 Answers0