Skip to content

Commit 7bd4be0

Browse files
arturovtthePunderWoman
authored andcommitted
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
1 parent ea5b737 commit 7bd4be0

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

packages/common/upgrade/src/location_shim.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,10 @@ export class $locationShim {
179179
}
180180
});
181181

182-
// update browser
182+
// Synchronize the browser's URL and state with the application.
183+
// Note: There is no need to save the `$watch` return value (deregister listener)
184+
// into a variable because `$scope.$$watchers` is automatically cleaned up when
185+
// the root scope is destroyed.
183186
$rootScope.$watch(() => {
184187
if (this.initializing || this.updateBrowser) {
185188
this.updateBrowser = false;
@@ -244,6 +247,14 @@ export class $locationShim {
244247
}
245248
this.$$replace = false;
246249
});
250+
251+
$rootScope.$on('$destroy', () => {
252+
// Complete the subject to release all active observers when the root
253+
// scope is destroyed. Before this change, we subscribed to the `urlChanges`
254+
// subject, and the subscriber captured `this`, leading to a memory leak
255+
// after the root scope was destroyed.
256+
this.urlChanges.complete();
257+
});
247258
}
248259

249260
private resetBrowserUpdate() {

0 commit comments

Comments
 (0)