Skip to content

Commit 8490e74

Browse files
committed
fix($rootScope): fix potential memory leak when removing scope listeners
When removing listeners the listener is removed from the array but the array size is not changed until the event is fired again. If that event is never fired but listeners are added/removed then this array will continue growing. This changes the listener removal to `delete` the array entry instead of setting it to `null` in the hope of the browser deallocating the memory for the array entry. Fixes angular#16135
1 parent 92739dc commit 8490e74

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/ng/rootScope.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,10 @@ function $RootScopeProvider() {
11801180
return function() {
11811181
var indexOfListener = namedListeners.indexOf(listener);
11821182
if (indexOfListener !== -1) {
1183-
namedListeners[indexOfListener] = null;
1183+
// Use delete in the hope of the browser deallocating the memory for the array entry,
1184+
// while not shifting the array indexes of other listeners.
1185+
// See issue https://github.com/angular/angular.js/issues/16135
1186+
delete namedListeners[indexOfListener];
11841187
decrementListenerCount(self, 1, name);
11851188
}
11861189
};

0 commit comments

Comments
 (0)