Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): memory leak if view container host view is destroyed while view ref is not #40219

Closed

Conversation

crisbeto
Copy link
Member

When we attach a ViewRef to a ViewContainerRef, we save a reference to the container onto the ViewRef so that we can remove it when the ref is destroyed. The problem is that if the container's hostView is destroyed first, the ViewRef has no way of knowing that it should stop referencing the container.

These changes remove the leak by not saving a reference at all. Instead, when a ViewRef is destroyed, we clean it up through the LContainer directly. We don't need to worry about the case where the container is destroyed before the view, because containers automatically clean up all of their views upon destruction.

Fixes #38648.

@google-cla google-cla bot added the cla: yes label Dec 21, 2020
@crisbeto crisbeto force-pushed the 38648-view-container-ref-leak branch 2 times, most recently from 7e2367c to 30d0d5b Compare December 21, 2020 11:08
@crisbeto crisbeto marked this pull request as ready for review December 21, 2020 11:44
@pullapprove pullapprove bot added the area: core Issues related to the framework runtime label Dec 21, 2020
@ngbot ngbot bot added this to the Backlog milestone Dec 21, 2020
@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer memory leak Issue related to a memory leak target: patch This PR is targeted for the next patch release labels Dec 21, 2020
… view ref is not

When we attach a `ViewRef` to a `ViewContainerRef`, we save a reference to the container
onto the `ViewRef` so that we can remove it when the ref is destroyed. The problem is
that if the container's `hostView` is destroyed first, the `ViewRef` has no way of knowing
that it should stop referencing the container.

These changes remove the leak by not saving a reference at all. Instead, when a `ViewRef`
is destroyed, we clean it up through the `LContainer` directly. We don't need to worry
about the case where the container is destroyed before the view, because containers
automatically clean up all of their views upon destruction.

Fixes angular#38648.
@crisbeto crisbeto force-pushed the 38648-view-container-ref-leak branch from 30d0d5b to f82f03d Compare January 6, 2021 06:09
"packages/core/src/error_handler.ts",
"packages/core/src/errors.ts",
"packages/core/src/view/types.ts",
"packages/core/src/linker/component_factory.ts"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always a good sign when bunch of circular refs get removed!

❤️

Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed-for: circular-dependencies

@AndrewKushnir AndrewKushnir added action: presubmit The PR is in need of a google3 presubmit and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Jan 7, 2021
@AndrewKushnir
Copy link
Contributor

Presubmit.

@crisbeto crisbeto added the action: merge The PR is ready for merge by the caretaker label Jan 7, 2021
@AndrewKushnir AndrewKushnir removed the action: presubmit The PR is in need of a google3 presubmit label Jan 7, 2021
atscott pushed a commit that referenced this pull request Jan 8, 2021
… view ref is not (#40219)

When we attach a `ViewRef` to a `ViewContainerRef`, we save a reference to the container
onto the `ViewRef` so that we can remove it when the ref is destroyed. The problem is
that if the container's `hostView` is destroyed first, the `ViewRef` has no way of knowing
that it should stop referencing the container.

These changes remove the leak by not saving a reference at all. Instead, when a `ViewRef`
is destroyed, we clean it up through the `LContainer` directly. We don't need to worry
about the case where the container is destroyed before the view, because containers
automatically clean up all of their views upon destruction.

Fixes #38648.

PR Close #40219
@atscott atscott closed this in 4f73820 Jan 8, 2021
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Feb 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: core Issues related to the framework runtime cla: yes memory leak Issue related to a memory leak target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug(core): viewRef._viewContainerRef can leak memory (ViewRef, ViewContainerRef)
3 participants