防止AudioWorkletNode内存泄漏

时间:2019-04-03 20:25:18

标签: javascript web-audio web-audio-api

我有一个AudioWorkletNode,它是一个类实例的成员。当我删除/杀死/删除该实例时,AudioWorkletNode和MessagePort会泄漏。

在删除实例之前,我确保相应的AudioWorkletProcessor的处理方法未运行。我也尝试过调用端口的close()方法,甚至将AudioWorkletNode设置为null。节点当时是连接还是断开似乎也无关紧要。无论哪种方式泄漏。

要建立audioWorklet模块:

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);
      });

   }
}

和处理器,出于相关性而被缩写为

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;
  }
}

在断开节点连接并删除AG_Recorder实例之前,我尝试执行以下操作:

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

0 个答案:

没有答案