Skip to content

Potential leak: ListView items not reused and not ngOnDestroy'ed on navigate #656

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

Closed
m-abs opened this issue Feb 6, 2017 · 1 comment · Fixed by #703
Closed

Potential leak: ListView items not reused and not ngOnDestroy'ed on navigate #656

m-abs opened this issue Feb 6, 2017 · 1 comment · Fixed by #703

Comments

@m-abs
Copy link
Contributor

m-abs commented Feb 6, 2017

We ran into this behavior in our App.
From #374 I know that page components are supposed to be reused when we open a new Page in our application and go back.

But that doesn't seem to apply to ListView items.
When we navigate away from the Page containing a ListView and back again.
New instances of the item components are created, but ngOnDestroy on the old components are never called.
Which could lead to leaks, since we cannot do clean up on destruction.

I've written an example here:
https://github.com/m-abs/ns-debugging/tree/ng2-list-view-no-reuse-post-navigate

If you checkout my example, you should see this behavior (on Android, haven't tested it on iOS):

0. Start the App.
  * You should see in the log:
    ListView(N)
    HomeItemComponent<1>.ngOnInit()
    HomeItemComponent<2>.ngOnInit()
    HomeItemComponent<.>.ngOnInit()
    HomeItemComponent<.>.ngOnInit()
    HomeItemComponent<.>.ngOnInit()
    HomeItemComponent<N>.ngOnInit()
1. Click on 'Read all about it'.
  * You should see in the log:
    No calls to ngOnDestroy on the HomeItemComponent elements.
2. Click back-button
  * You should see in the log:
    ListView(N) <--- Note same number e.g. same instance of the ListView
    HomeItemComponent<N+1>.ngOnInit()
    HomeItemComponent<N+2>.ngOnInit()
    HomeItemComponent<.>.ngOnInit()
    HomeItemComponent<.>.ngOnInit()
    HomeItemComponent<.>.ngOnInit()
    HomeItemComponent<N+N2>.ngOnInit()
@hdeshev
Copy link
Contributor

hdeshev commented Apr 4, 2017

We hit several crashes in our internal tests, and I had to reinvestigate this issue. It turns out that we are not supposed to destroy item views on element unloaded events since those were being fired when navigating to a new page. Destroying views there caused removal of items that were supposed to be recycled by the underlying ListView component, which is a major no-no.

Not destroying item views seems to be the correct behavior here since the list view items should stay loaded when navigating to a new page, and navigating back, should reattach the listview and all its items. ngOnDestroy will be called when you navigate back, or just remove the listview from the visual tree.

This uncovered another memory leak, which is probably what is the problem here. I am opening a new issue to track it. #737

@hdeshev hdeshev closed this as completed Apr 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants