Skip to content

Commit

Permalink
fix(tooltip): memory leak on show/hide
Browse files Browse the repository at this point in the history
Create a new child scope and retain a reference to it so that it can
be destroyed when the tooltip DOM is removed.

Fixes angular-ui#2709
Closes angular-ui#2919
  • Loading branch information
chrisirhc authored and Oron Nadiv committed Nov 18, 2014
1 parent 876991a commit 312a3be
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/tooltip/test/tooltip.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,19 @@ describe('tooltip', function() {
expect( elmBody.children().length ).toBe( 0 );
}));

it('issue 1191 - isolate scope on the popup should always be child of correct element scope', function () {
it('issue 1191 - scope on the popup should always be child of correct element scope', function () {
var ttScope;
elm.trigger( 'mouseenter' );

ttScope = angular.element( elmBody.children()[1] ).isolateScope();
ttScope = angular.element( elmBody.children()[1] ).scope();
expect( ttScope.$parent ).toBe( tooltipScope );

elm.trigger( 'mouseleave' );

// After leaving and coming back, the scope's parent should be the same
elm.trigger( 'mouseenter' );

ttScope = angular.element( elmBody.children()[1] ).isolateScope();
ttScope = angular.element( elmBody.children()[1] ).scope();
expect( ttScope.$parent ).toBe( tooltipScope );

elm.trigger( 'mouseleave' );
Expand Down Expand Up @@ -349,7 +349,7 @@ describe('tooltip', function() {
var match = false;

angular.forEach(angular.element.cache, function (item) {
if (item.data && item.data.$isolateScope === tooltipScope) {
if (item.data && item.data.$scope === tooltipScope) {
match = true;
}
});
Expand All @@ -369,7 +369,7 @@ describe('tooltip', function() {
tooltipScope = elmScope.$$childTail.$$childTail;
}));

it( 'should not contain a cached reference when visible', inject( function( $timeout ) {
it( 'should not contain a cached reference when not visible', inject( function( $timeout ) {
expect( inCache() ).toBeTruthy();
elmScope.$destroy();
expect( inCache() ).toBeFalsy();
Expand Down
8 changes: 7 additions & 1 deletion src/tooltip/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap

return function link ( scope, element, attrs ) {
var tooltip;
var tooltipLinkedScope;
var transitionTimeout;
var popupTimeout;
var appendToBody = angular.isDefined( options.appendToBody ) ? options.appendToBody : false;
Expand Down Expand Up @@ -234,7 +235,8 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
if (tooltip) {
removeTooltip();
}
tooltip = tooltipLinker(ttScope);
tooltipLinkedScope = ttScope.$new();
tooltip = tooltipLinker(tooltipLinkedScope);
}

function removeTooltip() {
Expand All @@ -243,6 +245,10 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
tooltip.remove();
tooltip = null;
}
if (tooltipLinkedScope) {
tooltipLinkedScope.$destroy();
tooltipLinkedScope = null;
}
}

function prepareTooltip() {
Expand Down

0 comments on commit 312a3be

Please sign in to comment.