From 57d3a635385a5b6f5f31f00c9ce52b18ff104b45 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Sun, 19 Nov 2017 23:41:42 +0100 Subject: [PATCH] Reduce leaks in NODEJS continue canvas creations (#4471) * reduce leaks in node * fix test --- src/canvas.class.js | 2 +- src/static_canvas.class.js | 7 ++++++- test/unit/canvas_static.js | 12 +++++++----- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/canvas.class.js b/src/canvas.class.js index 6b0c19b1505..9177bb42fe7 100644 --- a/src/canvas.class.js +++ b/src/canvas.class.js @@ -1538,7 +1538,6 @@ * @chainable */ dispose: function () { - fabric.StaticCanvas.prototype.dispose.call(this); var wrapper = this.wrapperEl; this.removeListeners(); wrapper.removeChild(this.upperCanvasEl); @@ -1548,6 +1547,7 @@ wrapper.parentNode.replaceChild(this.lowerCanvasEl, this.wrapperEl); } delete this.wrapperEl; + fabric.StaticCanvas.prototype.dispose.call(this); return this; }, diff --git a/src/static_canvas.class.js b/src/static_canvas.class.js index 4eb294d1e69..334bb0343ff 100644 --- a/src/static_canvas.class.js +++ b/src/static_canvas.class.js @@ -1664,7 +1664,12 @@ * @chainable */ dispose: function () { - this.clear(); + this._objects.length = 0; + this.backgroundImage = null; + this.overlayImage = null; + this._iTextInstances = null; + this.lowerCanvasEl = null; + this.cacheCanvasEl = null; return this; }, diff --git a/test/unit/canvas_static.js b/test/unit/canvas_static.js index 8ad411ea61d..d1c2222d1d5 100644 --- a/test/unit/canvas_static.js +++ b/test/unit/canvas_static.js @@ -1313,11 +1313,13 @@ assert.equal(canvas.toString(), '#'); }); - QUnit.test('dispose', function(assert) { - assert.ok(typeof canvas.dispose === 'function'); - canvas.add(makeRect(), makeRect(), makeRect()); - canvas.dispose(); - assert.equal(canvas.getObjects().length, 0, 'dispose should clear canvas'); + QUnit.test('dispose clear references', function(assert) { + var canvas2 = new fabric.Canvas(); + assert.ok(typeof canvas2.dispose === 'function'); + canvas2.add(makeRect(), makeRect(), makeRect()); + canvas2.dispose(); + assert.equal(canvas2.getObjects().length, 0, 'dispose should clear canvas'); + assert.equal(canvas2.lowerCanvasEl, null, 'dispose should clear lowerCanvasEl'); }); QUnit.test('clone', function(assert) {