简体   繁体   中英

Prevent AudioWorkletNode memory leak

I have an AudioWorkletNode that is a member of a class instance. When I delete/kill/remove that instance, the AudioWorkletNode and MessagePort leak.

Before deleting the instance, I make sure the corresponding AudioWorkletProcessor's process method isn't running. I've also tried calling the port's close() method, and even setting the AudioWorkletNode to null. It also doesn't seem to matter if the node is connected or disconnected at the time. It leaks either way.

To establish the audioWorklet module:

AG_Recorder = class AG_Recorder {

  constructor(outNode) {

      AG_AudioManager.audioCtx.audioWorklet.addModule( '/audioWorkers/recorder.js').then(() => {

        this.recorderNode = new AudioWorkletNode(AG_AudioManager.audioCtx, 'recorder');
        this.recorderNode.connect(outNode);

        this.recorderNode.port.onmessage = (event) => {
          this.handleMessage(event.data);
        };

      }).catch(function (err) {
        console.log('recorder audioworklet error: ', err.name, err.message);
      });

   }
}

And the processor, strongly abbreviated for relevancy:

class RecorderWorkletNode extends AudioWorkletProcessor {

  constructor (options) {

    super(options);
    this._running = true;

    this.port.onmessage = event => {

      if (event.data.release) {
        this._running = false;
      }
    }

    this.port.start();
  }

  process (inputs, outputs, parameters) {

    return this._running;
  }
}

And before the node is disconnected, and the AG_Recorder instance is deleted, I've tried doing this:

release() {
    this.recorderNode.port.postMessage({release: true});
    this.recorderNode.port.onmessage = null;
    this.recorderNode.port.close();
    this.recorderNode = null;
 }

It seems to be a confirmed bug for Chromium:

https://bugs.chromium.org/p/chromium/issues/detail?id=1298955

Update: not very sure about Firefox etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM