From 4687cc85acc3a374e70343a6732b7d97efa61de4 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 28 Feb 2019 12:05:11 +0100 Subject: [PATCH] Zero the width/height of the temporary canvas used during `JpegDecode` (issue 10594) --- src/display/api.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index 04eda7517..8041183ec 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -2068,15 +2068,14 @@ class WorkerTransport { return new Promise(function(resolve, reject) { const img = new Image(); img.onload = function() { - const width = img.width; - const height = img.height; + const { width, height, } = img; const size = width * height; const rgbaLength = size * 4; const buf = new Uint8ClampedArray(size * components); - const tmpCanvas = document.createElement('canvas'); + let tmpCanvas = document.createElement('canvas'); tmpCanvas.width = width; tmpCanvas.height = height; - const tmpCtx = tmpCanvas.getContext('2d'); + let tmpCtx = tmpCanvas.getContext('2d'); tmpCtx.drawImage(img, 0, 0); const data = tmpCtx.getImageData(0, 0, width, height).data; @@ -2092,6 +2091,13 @@ class WorkerTransport { } } resolve({ data: buf, width, height, }); + + // Zeroing the width and height cause Firefox to release graphics + // resources immediately, which can greatly reduce memory consumption. + tmpCanvas.width = 0; + tmpCanvas.height = 0; + tmpCanvas = null; + tmpCtx = null; }; img.onerror = function() { reject(new Error('JpegDecode failed to load image'));