IONIC WEBGL Out of memory on quit

Hi,
I’ve devloped an Ionic app which embed unity WEBGL build game. (2020+ version)
The unity loader.js script is embedded on Ionic app and the “game” is istanced from web.

const unityConfig: UnityConfig = {
codeUrl: this.exercise.URLWebGL + ‘/web.wasm.gz’,
dataUrl: this.exercise.URLWebGL + ‘/web.data.gz’,
frameworkUrl: this.exercise.URLWebGL + ‘/web.framework.js.gz’,
streamingAssetsUrl: ‘StreamingAssets’,
companyName: ‘’,
productName: ‘’,
productVersion: ‘’
};

await this.ngZone.runOutsideAngular(async () => {

this.unityInstance = await createUnityInstance(
document.querySelector(‘#unity-canvas’),
unityConfig,
(progress) => this.handleUnityProgress(progress)
);
});

The instance start correctly and work very well.
The problem is when I have to Quit the game.
When I call this.unityInstance.Quit() I get the following error on inspector:

exception thrown: TypeError: Cannot read properties of null (reading ‘onwheel’),TypeError: Cannot read properties of null (reading ‘onwheel’)
at _emscripten_set_wheel_callback_on_thread (https://dev.medico-amico.com/packets/ORTHOPEDIC/Build/web.framework.js.gz:3:222957)
at MainLoop() (https://dev.medico-amico.com/packets/ORTHOPEDIC/Build/web.wasm.gz:wasm-function[26635]:0x889a5f)
at dynCall_v (https://dev.medico-amico.com/packets/ORTHOPEDIC/Build/web.wasm.gz:wasm-function[37130]:0xb9c8d8)
at browserIterationFunc (https://dev.medico-amico.com/packets/ORTHOPEDIC/Build/web.framework.js.gz:3:217614)
at callUserCallback (https://dev.medico-amico.com/packets/ORTHOPEDIC/Build/web.framework.js.gz:3:170812)
at Object.runIter (https://dev.medico-amico.com/packets/ORTHOPEDIC/Build/web.framework.js.gz:3:172072)
at Browser_mainLoop_runner (https://dev.medico-amico.com/packets/ORTHOPEDIC/Build/web.framework.js.gz:3:170347)
at l.invokeTask (https://dev.medico-amico.com/polyfills-es2015.98a5e5b154a74c7172ec.js:1:7313)
at i.runTask (https://dev.medico-amico.com/polyfills-es2015.98a5e5b154a74c7172ec.js:1:2722)
at invokeTask (https://dev.medico-amico.com/polyfills-es2015.98a5e5b154a74c7172ec.js:1:8363)
at invoke (https://dev.medico-amico.com/polyfills-es2015.98a5e5b154a74c7172ec.js:1:8265)
at n.args. (https://dev.medico-amico.com/polyfills-es2015.98a5e5b154a74c7172ec.js:1:32237)

Uncaught TypeError: Cannot read properties of null (reading ‘onwheel’)
at _emscripten_set_wheel_callback_on_thread (web.framework.js.gz:3:222957)
at MainLoop() (web.wasm.gz:0x889a5f)
at dynCall_v (web.wasm.gz:0xb9c8d8)
at browserIterationFunc (web.framework.js.gz:3:217614)
at callUserCallback (web.framework.js.gz:3:170812)
at Object.runIter (web.framework.js.gz:3:172072)
at Browser_mainLoop_runner (web.framework.js.gz:3:170347)
at l.invokeTask (polyfills-es2015.98a5e5b154a74c7172ec.js:1:7313)
at i.runTask (polyfills-es2015.98a5e5b154a74c7172ec.js:1:2722)
at invokeTask (polyfills-es2015.98a5e5b154a74c7172ec.js:1:8363)
at invoke (polyfills-es2015.98a5e5b154a74c7172ec.js:1:8265)
at n.args. (polyfills-es2015.98a5e5b154a74c7172ec.js:1:32237)

and than generate continuosly the following error

Uncaught RuntimeError: memory access out of bounds
at DynamicVBOBufferManager::CleanupClass() (web.wasm.gz:0x93c8e6)
at CallbackArray::Invoke() (web.wasm.gz:0x73d86)
at MainLoop() (web.wasm.gz:0x887f37)
at MainLoopUpdateFromBackground(void*) (web.wasm.gz:0xf68985)
at dynCall_vi (web.wasm.gz:0xb9c774)
at web.framework.js.gz:3:215820
at web.framework.js.gz:3:215841
at l.invokeTask (polyfills-es2015.98a5e5b154a74c7172ec.js:1:7313)
at i.runTask (polyfills-es2015.98a5e5b154a74c7172ec.js:1:2722)
at invokeTask (polyfills-es2015.98a5e5b154a74c7172ec.js:1:8363)
at invoke (polyfills-es2015.98a5e5b154a74c7172ec.js:1:8265)
at n.args. (polyfills-es2015.98a5e5b154a74c7172ec.js:1:32237)

do you have any idea?

I forgot to write that if I use a webbrowser and load the unity webgl webassembly I get no error.

If I use the IONIC browser app I get to same error reported before

Same error for me on an angular project.
No solution yet…

It’s the same on React.

Bug report sent to Unity a -while- back.

See:
https://github.com/jeffreylanters/react-unity-webgl/issues/250

Will post again once I have a case # to get more traction going.

@brendanduncan_u3d @anthony_b - for your attention as this affects Unity integration in SPA frameworks.

Case 1381853 was submitted by creator of Unity-React plugin - but they didn’t properly follow-up with QA and case was closed. Issue is a regression in 2021.2, happens in 2022.1 too.

Simply put - removing the canvas of a WebGL build (changing page thus removing the component in a single page application “SPA” ala React) will throw a JS error.

Before 2021.2, Unity would gracefully quit when canvas was destroyed - not anymore.

Any solution for this? This happened to me too after upgrading my project to Unity 2021.
Would be great if this issue can be fixed soon.

Any news on this? It is a really annoying issue!

Here’s a workaround for v2021.3.4f1 - add this to webgl.jslib:

mergeInto(LibraryManager.library, {
  // override default callback
  emscripten_set_wheel_callback_on_thread: function (
    target,
    userData,
    useCapture,
    callbackfunc,
    targetThread
  ) {
    target = findEventTarget(target);

    // the fix
    if (!target) {
      return -4;
    }

    if (typeof target.onwheel !== 'undefined') {
      registerWheelEventCallback(
        target,
        userData,
        useCapture,
        callbackfunc,
        9,
        'wheel',
        targetThread
      );
      return 0;
    } else {
      return -1;
    }
  }
});
3 Likes

Please take a look here, found solution: Unity 2021.2 build throws error when removing the canvas before invoking Quit on the Unity Instance · Issue #22 · jeffreylanters/react-unity-webgl · GitHub

1 Like

Super obscure but great find. Thanks!

After applying this no error messages are shown when creating and shutting down unity instances. So far so good. However after closing a unity instance where this code gets called with the target null and -4 is returned, then when opening a new unity instance I can no longer navigate with right mouse client and mouse move, instead a context menu is shown. From added console logging it seems that the registration of the registerWheelEventCallback is the working in the same way as the first callup. Anyone have a solution for that?