rive-app / rive-wasm

Wasm/JS runtime for Rive
MIT License
739 stars 52 forks source link

Low-level API WebGL nonexistent cleanup function #368

Open joncardasis opened 2 months ago

joncardasis commented 2 months ago

Description

The @rive-app/webgl2-advanced@2.20.0 renderer (and other webgl renderers) exposes a cleanup function in .d.ts but errors when called: 'rive.cleanup' is not a function. See Repro section for an example usage.

It appears as though the wasm/js/*renderer.js files do not set a Module["cleanup"] function. console.log(Object.keys(rive)) does not contain any reference of cleanup.

This inability to cleanup the wasm allocations leads to memory leaks.

Provide a Repro

import Rive, {AABB, RiveCanvas, SMIInput} from '@rive-app/webgl2-advanced'

const rive = await Rive({
    locateFile: () =>
      `https://unpkg.com/@rive-app/webgl2-advanced@2.20.0/rive.wasm`,
  })
const bytes = await (await fetch(new Request(url))).arrayBuffer()
const file = await rive.load(new Uint8Array(bytes))

const renderer = rive.makeRenderer(canvas, !isFirefox())
const artboard = file.artboardByName("file-artboard-name")

function renderLoop(time: DOMHighResTimeStamp) {
  ...
}

requestFrameId = rive.requestAnimationFrame(renderLoop)

return {
  cleanup: function () { 
    rive.cleanup()
  }
}
joncardasis commented 2 months ago

Also to note that https://rive.app/community/doc/low-level-api-usage/doctAfBY6v3P#cleaning-up-instances outlines deleting resources manually, however, renderer.delete(); also throws an error where delete is not a function on the webgl2 renderer.

renderer.delete();  // Error – renderer.delete is not a function
file.delete();
artboard.delete();
animation.delete();
stateMachine.delete();