locked
[UWP] Anniversary Update GC no longer reclaims bound appbar buttons. RRS feed

  • Question

  • Hi,

    When I updated my build to target anniversary update, I noticed it started leaking memory. I finally managed to find the cause for this, which seems to be related to AppBarButtons preventing ViewModels from being reclaimed by GC. I managed to create a small test project which minimally can illustrate the problem. It can be found here:
    https://www.dropbox.com/s/hu0wcr4e5ft18na/BindingAppBarButtons.zip?dl=0


    The way the problem manifests itself is as follows:

    I have a Page with the following in the XAML (Page1.xaml).

        <Page.TopAppBar>
            <CommandBar>
                <AppBarButton Label="Left" Icon="AlignLeft" Command="{Binding SaySomethingCommand}" CommandParameter="Left"/>
                <AppBarButton Label="Middle" Icon="AlignCenter" Command="{Binding SaySomethingCommand}" CommandParameter="Middle"/>
                <AppBarButton Label="Right" Icon="AlignRight" Command="{Binding SaySomethingCommand}" CommandParameter="Right"/>
            </CommandBar>
        </Page.TopAppBar>

    (It appears that AppBarButtons inside <Commandbar.Conent> are fine)

    The ViewModel that contains the SaySomethingCommand will not be reclaimed when navigating away from the page, provided the DataContext is left. It is worth mentioning that a regular button as part of the page can bind to the same Command without any problems.

    In the sample attached, in order to have the ViewModel reclaimed, 1 of 3 things can be done.

    1. In Page1.xaml.cs, set this.DataContext = null; in OnNavigatedFrom.

    2. Remove the AppBarButtons.

    3. Change the project to target 10586 instead of 14393.

    Maybe I am missing some best practices in my project, but it doesn't seem like this is intended behaviour. And to avoid and unnecessary back-and-forth, I am aware that GC is not deterministic. The sample prints to the console when Page1ViewModel is reclaimed by GC, and when you apply one of the 3 fixes, it always gets a round of reclamation before the 10th navigation has happened, while I can get to 30 easily without a reclaim if I don't have the fix applied.

    Best Regards,
    Tomas Hofmann



    • Edited by T Hofmann Tuesday, March 21, 2017 11:43 PM
    Tuesday, March 21, 2017 11:37 PM

Answers

  • Hi Tomas,

    this is a known bug that has been fixed for the upcoming Creator's update.

    Thanks!
    Stefan Wick


    Wednesday, March 22, 2017 1:54 AM

All replies

  • Hi Tomas,

    this is a known bug that has been fixed for the upcoming Creator's update.

    Thanks!
    Stefan Wick


    Wednesday, March 22, 2017 1:54 AM
  • Hi Stefan,

    Thanks for letting me know. I guess I could have saved myself a lot of time if I had known that.

    Best Regards,
    Tomas Hofmann

    Wednesday, March 22, 2017 4:08 PM