Skip to content

Commit

Permalink
fix potential memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
pissang committed Jul 27, 2021
1 parent 0ea0ee9 commit 26f9e53
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/chart/bar3D/Bar3DView.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ export default echarts.ChartView.extend({
},

dispose: function () {
this._labelsBuilder.dispose();
this.groupGL.removeAll();
}
});
4 changes: 4 additions & 0 deletions src/chart/common/PointsBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ PointsBuilder.prototype = {
this.rootNode.add(this._labelsBuilder.getMesh());
},

dispose: function () {
this._labelsBuilder.dispose();
},

_updateSymbolSprite: function (seriesModel, itemStyle, symbolInfo, dpr) {
symbolInfo.maxSize = Math.min(symbolInfo.maxSize * 2, 200);
var symbolSize = [];
Expand Down
2 changes: 2 additions & 0 deletions src/chart/graphGL/GraphGLView.js
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,8 @@ export default echarts.ChartView.extend({

// Stop layout.
this._layoutId = -1;

this._pointsBuilder.dispose();
},

remove: function () {
Expand Down
1 change: 1 addition & 0 deletions src/chart/map3D/Map3DView.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,6 @@ export default echarts.ChartView.extend({
dispose: function () {
this.groupGL.removeAll();
this._control.dispose();
this._geo3DBuilder.dispose();
}
});
3 changes: 3 additions & 0 deletions src/chart/polygons3D/Polygons3DView.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,8 @@ export default echarts.ChartView.extend({

dispose: function () {
this.groupGL.removeAll();
this._geo3DBuilderList.forEach(function (geo3DBuilder) {
geo3DBuilder.dispose();
})
}
});
3 changes: 3 additions & 0 deletions src/chart/scatter3D/Scatter3DView.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ export default echarts.ChartView.extend({
},

dispose: function () {
this._pointsBuilderList.forEach(function (pointsBuilder) {
pointsBuilder.dispose();
});
this.groupGL.removeAll();
},

Expand Down
3 changes: 3 additions & 0 deletions src/chart/scatterGL/ScatterGLView.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ export default echarts.ChartView.extend({

dispose: function () {
this.groupGL.removeAll();
this._pointsBuilderList.forEach(function (pointsBuilder) {
pointsBuilder.dispose();
});
},

remove: function () {
Expand Down
4 changes: 4 additions & 0 deletions src/component/common/Geo3DBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,10 @@ Geo3DBuilder.prototype = {
this._setColorOfDataIndex(data, dataIndex, colorArr);
},

dispose: function () {
this._labelsBuilder.dispose();
},

_setColorOfDataIndex: function (data, dataIndex, colorArr) {
if (dataIndex < this._startIndex && dataIndex > this._endIndex) {
return;
Expand Down
4 changes: 4 additions & 0 deletions src/component/common/LabelsBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,8 @@ LabelsBuilder.prototype.updateLabels = function (highlightDataIndices) {
this._labelsMesh.geometry.dirty();
};

LabelsBuilder.prototype.dispose = function () {
this._labelTextureSurface.dispose();
}

export default LabelsBuilder;
1 change: 1 addition & 0 deletions src/component/geo3D/Geo3DView.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,6 @@ export default echarts.ComponentView.extend({

dispose: function () {
this._control.dispose();
this._geo3DBuilder.dispose();
}
});
2 changes: 2 additions & 0 deletions src/component/grid3D/Grid3DView.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,5 +581,7 @@ export default echarts.ComponentView.extend({
dispose: function () {
this.groupGL.removeAll();
this._control.dispose();
this._axisLabelSurface.dispose();
this._axisPointerLabelsSurface.dispose();
}
});
17 changes: 12 additions & 5 deletions src/core/LayerGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
* (renderer) (renderer)
* / \
* ViewGL ViewGL
*
* @module echarts-gl/core/LayerGL
* @author Yi Shen(http://github.com/pissang)
*/

import * as echarts from 'echarts/lib/echarts';
Expand Down Expand Up @@ -103,6 +100,8 @@ var LayerGL = function (id, zr) {
});

this._backgroundColor = null;

this._disposed = false;
};

LayerGL.prototype.setUnpainted = function () {};
Expand Down Expand Up @@ -419,10 +418,18 @@ function collectResources(scene, textureResourceList, geometryResourceList) {
* Dispose the layer
*/
LayerGL.prototype.dispose = function () {
if (this._disposed) {
return;
}
this._stopAccumulating();
this.renderer.disposeScene(this.scene);

if (this._textureList) {
markUnused(this._textureList);
markUnused(this._geometriesList);
checkAndDispose(this.renderer, this._textureList);
checkAndDispose(this.renderer, this._geometriesList);
}
this.zr.off('globalout', this.onglobalout);
this._disposed = true;
};

// Event handlers
Expand Down
13 changes: 12 additions & 1 deletion src/echarts-gl.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,18 @@ EChartsGL.prototype.update = function (ecModel, api) {
// TODO

echarts.registerPostInit(function (chart) {
chart.getZr().painter.getRenderedCanvas = function (opts) {
var zr = chart.getZr();
var oldDispose = zr.painter.dispose;

zr.painter.dispose = function () {
this.eachOtherLayer(function (layer) {
if (layer instanceof LayerGL) {
layer.dispose();
}
});
oldDispose.call(this);
}
zr.painter.getRenderedCanvas = function (opts) {
opts = opts || {};
if (this._singleCanvas) {
return this._layers[0].dom;
Expand Down
4 changes: 4 additions & 0 deletions src/util/ZRTextureAtlasSurface.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ ZRTextureAtlasSurface.prototype = {
*/
getCoords: function (id) {
return this._coords[id];
},

dispose: function () {
this._zr.dispose();
}
};

Expand Down
1 change: 1 addition & 0 deletions test/js/commonUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
btn.onclick = function () {
(typeof myChart !== 'undefined') && myChart.dispose();
(typeof chart !== 'undefined') && chart.dispose();
window.onresize = null;
}

document.body.appendChild(btn);
Expand Down

0 comments on commit 26f9e53

Please sign in to comment.