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

Commit 528cf09

Browse files
realitykingpetebacondarwin
authored andcommitted
fix(rootScope): prevent memory leak when destroying scopes
Closes #11173 Closes #11169
1 parent c849098 commit 528cf09

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/ng/rootScope.js

+20-14
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,27 @@ function $RootScopeProvider() {
8080
return TTL;
8181
};
8282

83+
function createChildScopeClass(parent) {
84+
function ChildScope() {
85+
this.$$watchers = this.$$nextSibling =
86+
this.$$childHead = this.$$childTail = null;
87+
this.$$listeners = {};
88+
this.$$listenerCount = {};
89+
this.$$watchersCount = 0;
90+
this.$id = nextUid();
91+
this.$$ChildScope = null;
92+
}
93+
ChildScope.prototype = parent;
94+
return ChildScope;
95+
}
96+
8397
this.$get = ['$injector', '$exceptionHandler', '$parse', '$browser',
8498
function($injector, $exceptionHandler, $parse, $browser) {
8599

100+
function destroyChildScope($event) {
101+
$event.currentScope.$$destroyed = true;
102+
}
103+
86104
/**
87105
* @ngdoc type
88106
* @name $rootScope.Scope
@@ -205,15 +223,7 @@ function $RootScopeProvider() {
205223
// Only create a child scope class if somebody asks for one,
206224
// but cache it to allow the VM to optimize lookups.
207225
if (!this.$$ChildScope) {
208-
this.$$ChildScope = function ChildScope() {
209-
this.$$watchers = this.$$nextSibling =
210-
this.$$childHead = this.$$childTail = null;
211-
this.$$listeners = {};
212-
this.$$listenerCount = {};
213-
this.$id = nextUid();
214-
this.$$ChildScope = null;
215-
};
216-
this.$$ChildScope.prototype = this;
226+
this.$$ChildScope = createChildScopeClass(this);
217227
}
218228
child = new this.$$ChildScope();
219229
}
@@ -231,13 +241,9 @@ function $RootScopeProvider() {
231241
// prototypically. In all other cases, this property needs to be set
232242
// when the parent scope is destroyed.
233243
// The listener needs to be added after the parent is set
234-
if (isolate || parent != this) child.$on('$destroy', destroyChild);
244+
if (isolate || parent != this) child.$on('$destroy', destroyChildScope);
235245

236246
return child;
237-
238-
function destroyChild() {
239-
child.$$destroyed = true;
240-
}
241247
},
242248

243249
/**

0 commit comments

Comments
 (0)