mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
fix(common): clean up urlChanges subscribers when root scope is destroyed (#59703)
In this commit, the `urlChanges` subject is completed to release all active observers when the root scope is destroyed. Previously, subscribing to the `urlChanges` subject caused the subscriber to capture `this`, resulting in a memory leak after the root scope was destroyed. PR Close #59703
This commit is contained in:
parent
ea5b7371e5
commit
7bd4be0fa5
1 changed files with 12 additions and 1 deletions
|
|
@ -179,7 +179,10 @@ export class $locationShim {
|
|||
}
|
||||
});
|
||||
|
||||
// update browser
|
||||
// Synchronize the browser's URL and state with the application.
|
||||
// Note: There is no need to save the `$watch` return value (deregister listener)
|
||||
// into a variable because `$scope.$$watchers` is automatically cleaned up when
|
||||
// the root scope is destroyed.
|
||||
$rootScope.$watch(() => {
|
||||
if (this.initializing || this.updateBrowser) {
|
||||
this.updateBrowser = false;
|
||||
|
|
@ -244,6 +247,14 @@ export class $locationShim {
|
|||
}
|
||||
this.$$replace = false;
|
||||
});
|
||||
|
||||
$rootScope.$on('$destroy', () => {
|
||||
// Complete the subject to release all active observers when the root
|
||||
// scope is destroyed. Before this change, we subscribed to the `urlChanges`
|
||||
// subject, and the subscriber captured `this`, leading to a memory leak
|
||||
// after the root scope was destroyed.
|
||||
this.urlChanges.complete();
|
||||
});
|
||||
}
|
||||
|
||||
private resetBrowserUpdate() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue