From 7bd4be0fa585fda8a09d27683ade77b383500768 Mon Sep 17 00:00:00 2001 From: arturovt Date: Fri, 24 Jan 2025 16:38:28 +0200 Subject: [PATCH] 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 --- packages/common/upgrade/src/location_shim.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/common/upgrade/src/location_shim.ts b/packages/common/upgrade/src/location_shim.ts index 479b2eca8c6..5b6c7bb7ca3 100644 --- a/packages/common/upgrade/src/location_shim.ts +++ b/packages/common/upgrade/src/location_shim.ts @@ -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() {