From a0e947cd5b2e8e00351f8c1ff5ca2367c2f555dc Mon Sep 17 00:00:00 2001 From: Tim Roes Date: Thu, 18 Oct 2018 15:49:48 +0200 Subject: [PATCH] Fix memory leak in vislib on auto refresh (#24134) --- .../public/vis/vis_types/vislib_vis_type.js | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/ui/public/vis/vis_types/vislib_vis_type.js b/src/ui/public/vis/vis_types/vislib_vis_type.js index d5bd89d7c58095..f264279eee0ce8 100644 --- a/src/ui/public/vis/vis_types/vislib_vis_type.js +++ b/src/ui/public/vis/vis_types/vislib_vis_type.js @@ -44,6 +44,7 @@ export function VislibVisTypeProvider(Private, $rootScope, $timeout, $compile) { constructor(el, vis) { this.el = el; this.vis = vis; + this.$scope = null; this.container = document.createElement('div'); this.container.className = 'vislib-container'; @@ -65,20 +66,19 @@ export function VislibVisTypeProvider(Private, $rootScope, $timeout, $compile) { return resolve(); } - let $scope; if (this.vis.params.addLegend) { $(this.container).attr('class', (i, cls) => { return cls.replace(/vislib-container--legend-\S+/g, ''); }).addClass(legendClassName[this.vis.params.legendPosition]); - $scope = $rootScope.$new(); - $scope.refreshLegend = 0; - $scope.vis = this.vis; - $scope.visData = esResponse; - $scope.uiState = $scope.vis.getUiState(); - const legendHtml = $compile('')($scope); + this.$scope = $rootScope.$new(); + this.$scope.refreshLegend = 0; + this.$scope.vis = this.vis; + this.$scope.visData = esResponse; + this.$scope.uiState = this.$scope.vis.getUiState(); + const legendHtml = $compile('')(this.$scope); this.container.appendChild(legendHtml[0]); - $scope.$digest(); + this.$scope.$digest(); } this.vis.vislibVis = new vislib.Vis(this.chartEl, this.vis.params); @@ -88,8 +88,8 @@ export function VislibVisTypeProvider(Private, $rootScope, $timeout, $compile) { this.vis.vislibVis.render(esResponse, this.vis.getUiState()); if (this.vis.params.addLegend) { - $scope.refreshLegend++; - $scope.$digest(); + this.$scope.refreshLegend++; + this.$scope.$digest(); } }); } @@ -102,6 +102,10 @@ export function VislibVisTypeProvider(Private, $rootScope, $timeout, $compile) { delete this.vis.vislibVis; } $(this.container).find('vislib-legend').remove(); + if (this.$scope) { + this.$scope.$destroy(); + this.$scope = null; + } } }