我有一个 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;
}
最佳答案
这似乎是 Chromium 的一个已确认错误:
https://bugs.chromium.org/p/chromium/issues/detail?id=1298955
更新:不太确定 Firefox 等
关于javascript - 防止 AudioWorkletNode 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55503719/