From 749cdcd195f51e29dc6f4a280ee4c3ef049e950e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Billioud?= Date: Thu, 31 Aug 2023 03:59:37 +0200 Subject: [PATCH] Cleanup iframe first to prevent memory leaks, see #1609 --- src/dom/document-cloner.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/dom/document-cloner.ts b/src/dom/document-cloner.ts index 46915330f..6e7dac43e 100644 --- a/src/dom/document-cloner.ts +++ b/src/dom/document-cloner.ts @@ -220,7 +220,7 @@ export class DocumentCloner { clonedCanvas.width = canvas.width; clonedCanvas.height = canvas.height; const ctx = canvas.getContext('2d'); - const clonedCtx = clonedCanvas.getContext('2d', { willReadFrequently: true }); + const clonedCtx = clonedCanvas.getContext('2d', {willReadFrequently: true}); if (clonedCtx) { if (!this.options.allowTaint && ctx) { clonedCtx.putImageData(ctx.getImageData(0, 0, canvas.width, canvas.height), 0, 0); @@ -479,10 +479,24 @@ export class DocumentCloner { } static destroy(container: HTMLIFrameElement): boolean { + try { + // Clear the iframe's content + container.src = 'about:blank'; + + // Optionally allow the browser to handle garbage collection + if (container.contentWindow) { + container.contentWindow.document.open(); + container.contentWindow.document.write(''); + container.contentWindow.document.close(); + } + } catch {} + + // Remove the iframe from the DOM if (container.parentNode) { container.parentNode.removeChild(container); return true; } + return false; } }