Fix race condition when removing canvas before it has rendered (#6033)

Fixes #6030: Cancel animation frame request before removing the map.
This fixes the bug where the the delayed call to _redraw is trying to get the drawing context which has already been destroyed.
This commit is contained in:
louMoxy 2018-01-29 14:36:50 +00:00 committed by Iván Sánchez Ortega
parent c3a6a5b60a
commit 3e8e76c790
2 changed files with 47 additions and 0 deletions

View File

@ -190,3 +190,49 @@ describe('Canvas', function () {
}); });
}); });
describe('Canvas remove', function () {
var c;
before(function () {
c = document.createElement('div');
c.style.width = '400px';
c.style.height = '400px';
c.style.position = 'absolute';
c.style.top = '0';
c.style.left = '0';
document.body.appendChild(c);
});
after(function () {
document.body.removeChild(c);
});
function createCanvasMap(c, options) {
var map = new L.Map(c, options);
map.setView([0, 0], 6);
var p2ll = function (x, y) {
return map.layerPointToLatLng([x, y]);
};
var latLngs = [p2ll(0, 0), p2ll(0, 100), p2ll(100, 100), p2ll(100, 0)];
var layer = L.polygon(latLngs).addTo(map);
return map;
}
it("can remove the map without errors", function (done) {
var map1 = createCanvasMap(c, {preferCanvas: true, zoomControl: false});
map1.remove();
L.Util.requestAnimFrame(function () { done(); });
});
it("can remove renderer without errors", function (done) {
var canvas = L.canvas();
var map = createCanvasMap(c, {renderer: canvas, zoomControl: false});
canvas.remove();
map.remove();
L.Util.requestAnimFrame(function () { done(); });
});
});

View File

@ -68,6 +68,7 @@ export var Canvas = Renderer.extend({
}, },
_destroyContainer: function () { _destroyContainer: function () {
Util.cancelAnimFrame(this._redrawRequest);
delete this._ctx; delete this._ctx;
DomUtil.remove(this._container); DomUtil.remove(this._container);
DomEvent.off(this._container); DomEvent.off(this._container);