Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix(rootScope): prevent memory leak when destroying scopes
Browse files Browse the repository at this point in the history
Closes #11173
Closes #11169
  • Loading branch information
realityking authored and petebacondarwin committed Mar 9, 2015
1 parent c849098 commit 528cf09
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,27 @@ function $RootScopeProvider() {
return TTL;
};

function createChildScopeClass(parent) {
function ChildScope() {
this.$$watchers = this.$$nextSibling =
this.$$childHead = this.$$childTail = null;
this.$$listeners = {};
this.$$listenerCount = {};
this.$$watchersCount = 0;
this.$id = nextUid();
this.$$ChildScope = null;
}
ChildScope.prototype = parent;
return ChildScope;
}

this.$get = ['$injector', '$exceptionHandler', '$parse', '$browser',
function($injector, $exceptionHandler, $parse, $browser) {

function destroyChildScope($event) {
$event.currentScope.$$destroyed = true;
}

/**
* @ngdoc type
* @name $rootScope.Scope
Expand Down Expand Up @@ -205,15 +223,7 @@ function $RootScopeProvider() {
// Only create a child scope class if somebody asks for one,
// but cache it to allow the VM to optimize lookups.
if (!this.$$ChildScope) {
this.$$ChildScope = function ChildScope() {
this.$$watchers = this.$$nextSibling =
this.$$childHead = this.$$childTail = null;
this.$$listeners = {};
this.$$listenerCount = {};
this.$id = nextUid();
this.$$ChildScope = null;
};
this.$$ChildScope.prototype = this;
this.$$ChildScope = createChildScopeClass(this);
}
child = new this.$$ChildScope();
}
Expand All @@ -231,13 +241,9 @@ function $RootScopeProvider() {
// prototypically. In all other cases, this property needs to be set
// when the parent scope is destroyed.
// The listener needs to be added after the parent is set
if (isolate || parent != this) child.$on('$destroy', destroyChild);
if (isolate || parent != this) child.$on('$destroy', destroyChildScope);

return child;

function destroyChild() {
child.$$destroyed = true;
}
},

/**
Expand Down

2 comments on commit 528cf09

@SamuraiPrinciple
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No big deal, but you may have accidentally included

this.$$watchersCount = 0;

in the 1.3 branch (I believe $$watchersCount was introduced in 1.4)

@petebacondarwin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great spot. Fixed at bbc5fdd
Thanks

Please sign in to comment.