Skip to content

Commit

Permalink
Better resource cleaning on canvas dispose (fabricjs#5142)
Browse files Browse the repository at this point in the history
* test-leak

* better than nothing

* avoid breaking tests

* avoid breaking tests
  • Loading branch information
asturur authored Aug 5, 2018
1 parent 507388f commit fb44937
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -1577,10 +1577,12 @@
this.removeListeners();
wrapper.removeChild(this.upperCanvasEl);
wrapper.removeChild(this.lowerCanvasEl);
this.upperCanvasEl = null;
this.cacheCanvasEl = null;
this.contextCache = null;
this.contextTop = null;
['upperCanvasEl', 'cacheCanvasEl'].forEach((function(element) {
fabric.util.cleanUpJsdomNode(this[element]);
this[element] = undefined;
}).bind(this));
if (wrapper.parentNode) {
wrapper.parentNode.replaceChild(this.lowerCanvasEl, this.wrapperEl);
}
Expand Down
8 changes: 5 additions & 3 deletions src/shapes/image.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,11 @@
backend.evictCachesForKey(this.cacheKey);
backend.evictCachesForKey(this.cacheKey + '_filtered');
}
this._originalElement = undefined;
this._element = undefined;
this._filteredEl = undefined;
this._cacheContext = undefined;
['_originalElement', '_element', '_filteredEl', '_cacheCanvas'].forEach((function(element) {
fabric.util.cleanUpJsdomNode(this[element]);
this[element] = undefined;
}).bind(this));
},

/**
Expand Down
9 changes: 8 additions & 1 deletion src/static_canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -1673,11 +1673,18 @@
object.dispose && object.dispose();
});
this._objects = [];
if (this.backgroundImage && this.backgroundImage.dispose) {
this.backgroundImage.dispose();
}
this.backgroundImage = null;
if (this.overlayImage && this.overlayImage.dispose) {
this.overlayImage.dispose();
}
this.overlayImage = null;
this._iTextInstances = null;
this.lowerCanvasEl = null;
this.contextContainer = null;
fabric.util.cleanUpJsdomNode(this.lowerCanvasEl);
this.lowerCanvasEl = undefined;
return this;
},

Expand Down
16 changes: 16 additions & 0 deletions src/util/dom_misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,21 @@
return impl._canvas || impl._image;
};

function cleanUpJsdomNode(element) {
if (!fabric.isLikelyNode) {
return;
}
var impl = fabric.jsdomImplForWrapper(element);
if (impl) {
impl._image = null;
impl._canvas = null;
// unsure if necessary
impl._currentSrc = null;
impl._attributes = null;
impl._classList = null;
}
}

fabric.util.getById = getById;
fabric.util.toArray = toArray;
fabric.util.makeElement = makeElement;
Expand All @@ -301,5 +316,6 @@
fabric.util.getElementOffset = getElementOffset;
fabric.util.getElementStyle = getElementStyle;
fabric.util.getNodeCanvas = getNodeCanvas;
fabric.util.cleanUpJsdomNode = cleanUpJsdomNode;

})();
6 changes: 4 additions & 2 deletions test/visual/resize_filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@
}
var img = fabric.document.createElement('img');
img.onload = function() {
img.onload = null;
callback(img);
};
img.onerror = function(err) {
img.onerror = null;
console.log('Image loading errored', err);
};
img.src = filename;
Expand Down Expand Up @@ -79,8 +81,8 @@
image.scaleToWidth(canvas.width / zoom);
canvas.add(image);
canvas.renderAll();
image.dispose();
callback(canvas.lowerCanvasEl);
image.dispose();
});
}

Expand All @@ -103,8 +105,8 @@
image.scaleToWidth(canvas.width);
canvas.add(image);
canvas.renderAll();
image.dispose();
callback(canvas.lowerCanvasEl);
image.dispose();
});
}

Expand Down

0 comments on commit fb44937

Please sign in to comment.