ng-metadata icon indicating copy to clipboard operation
ng-metadata copied to clipboard

fix(async pipe): prevent firing $digest after scope is destroyed.

Open SpeedShifter opened this issue 6 years ago • 0 comments

Issue: async pipe could trigger digest after scope was destroyed. It could result in memory leak, because digest will trigger async pipe (like a filter), making it to subscribe observable in already destroyed scope (and $destroy won't happen again). The source of the problem for async pipe is _markForCheck method, that sets timeout, and triggers $digest without checking if scope is destroyed. Solution: track timeouts and clean them while _dispose and before setting new timeout.

For test, set of async-destroy components were created. Clicking 'start' button dispatches change in Subject imitating application state. If 'Memory leak' (when digest called after scope being destroyed) happened, alert message will be shown. To test fail scenario comment clearTimeout lines if async pipe. The key point is "middle" async-destroy-container component that on $destroy triggers synchronous change of Subject. Thats a scenario when component on destroy want to change the state of application.

SpeedShifter avatar May 18 '18 10:05 SpeedShifter