Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

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 #2709
Closes #2919
  • Loading branch information
chrisirhc committed Nov 5, 2014
1 parent 8204c80 commit faf38d2
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 faf38d2

Please sign in to comment.