Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] VisualStateManager memory leak when set in a global ResourceDictionary #14815

Open
jsclay opened this issue Nov 1, 2021 · 4 comments
Open

Comments

@jsclay
Copy link

jsclay commented Nov 1, 2021

Description

VisualStateManger (VSM) creates a memory leak when set in a ResourceDictionary added to Application's resources (e.g. Application.Current.Resources.MergedDictionaries.Add(new StyleResources()); )

For example, say you have a ResourceDictionary that holds application-wide styles and styleclasses. And in this dictionary you have a 'RedButton' styleclass that utilizes VSM. Any page that uses this 'RedButton' styleclass will leak.

static void VisualStateGroupsPropertyChanged(BindableObject bindable, object oldValue, object newValue)

We believe we have found the source of this leak in the previous link. In VSM's code, line 25, there is a 'VisualStateGroupsPropertyChanged' handler that sets the VisualStateGroupList's VisualElement to null when the VisualStateGroups property changes. This is the only place it gets set to null. So in the instance of VisualStateGroup being set in a global ResourceDictionary that doesn't get collected until the end of the application, nothing's triggering that VisualStateGroupsPropertyChanged handler upon leaving the page, meaning the VisualStateGroup hangs out in memory resulting in the memory leak.

Note: as demonstrated by my linked sample project below, setting a VSM at page/control level does not lead to memory leak. This is likely because when the page/control gets collected, it must trigger VisualStateGroupsPropertyChanged, cleaning up the VisualStateGroupList.

Another note: Setting a blank VSM in a global ResourceDictionary will not cause the memory leak for us. The VSM must have states defined in it for the leak to occur. Again, this points to the issue being with VisualStateGroupsPropertyChanged as a blank VSM (in our case at least) does not trigger any changes, therefore VisualStateGroupsPropertyChanged doesn't get hit, resulting in no VisualStateGroupList getting set (so there's nothing to collect).

To demonstrate the above note, the following code does not leak.

        <Setter Property="VisualStateManager.VisualStateGroups">
            <VisualStateGroupList>
                <VisualStateGroup x:Name="CommonStates">
                  
                </VisualStateGroup>
            </VisualStateGroupList>
        </Setter>

But this one does leak

        <Setter Property="VisualStateManager.VisualStateGroups">
            <VisualStateGroupList>
                <VisualStateGroup x:Name="CommonStates">

                    <VisualState x:Name="Normal">
                        <VisualState.Setters>
                            <Setter Property="Opacity" Value="1" />
                        </VisualState.Setters>
                    </VisualState>

                    <VisualState x:Name="Focused">
                        <VisualState.Setters>
                            <Setter Property="Opacity" Value="1" />
                        </VisualState.Setters>
                    </VisualState>

                    <VisualState x:Name="Disabled">
                        <VisualState.Setters>
                            <Setter Property="Opacity" Value=".7" />
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateGroupList>
        </Setter>

Steps to Reproduce

  1. Create a style/styleclass in a ResourceDictionary that sets the VisualStateManager.
  2. Add this ResourceDictionary to Application.Current.Resources
  3. Create multiple pages (3 min for this example) and put debug statements in the finalizers
  4. Apply this style/styleclass to a control on one of the pages (this will be the 'leaky page').
  5. Run app, navigate to this leaky page
  6. Navigate off to another page and back-and-forth between non-leaky pages.
  7. Notice that the debug statement for the leaky page doesn't show up in the output.

Note: in our (closed-source) app, navigating away from a leaky page then later back to it will deconstruct the first instance of this page. I was not able to re-create that in my sample project...our navigation service must have handled multiple instances of a page. To be safe however, when testing, I recommend navigating to the leaky page just once, and navigating between other pages to build pressure and see if the leaky page collects.

Basic Information

  • Version with issue: 5.0.0.2012 (originally discovered in 4.5.0.396)
  • Last known good version: N/A
  • NuGet Packages: MvvmLightLibs

Environment

Show/Hide Visual Studio info
Microsoft Visual Studio Professional 2019
Version 16.11.5
VisualStudio.16.Release/16.11.5+31729.503
Microsoft .NET Framework
Version 4.8.03752

Installed Version: Professional

Visual C++ 2019   00435-60000-00000-AA470
Microsoft Visual C++ 2019

ADL Tools Service Provider   1.0
This package contains services used by Data Lake tools

ASA Service Provider   1.0

ASP.NET and Web Tools 2019   16.11.75.64347
ASP.NET and Web Tools 2019

ASP.NET Web Frameworks and Tools 2019   16.11.75.64347
For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0   16.11.75.64347
Azure App Service Tools v3.0.0

Azure Data Lake Node   1.0
This package contains the Data Lake integration nodes for Server Explorer.

Azure Data Lake Tools for Visual Studio   2.6.1000.0
Microsoft Azure Data Lake Tools for Visual Studio

Azure Functions and Web Jobs Tools   16.11.75.64347
Azure Functions and Web Jobs Tools

Azure Stream Analytics Tools for Visual Studio   2.6.1000.0
Microsoft Azure Stream Analytics Tools for Visual Studio

C# Tools   3.11.0-4.21403.6+ae1fff344d46976624e68ae17164e0607ab68b10
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools   1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Cookiecutter   16.11.21196.2
Provides tools for finding, instantiating and customizing templates in cookiecutter format.

Extensibility Message Bus   1.2.6 (master@34d6af2)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

Fabric.DiagnosticEvents   1.0
Fabric Diagnostic Events

IntelliCode Extension   1.0
IntelliCode Visual Studio Extension Detailed Info

Linux Core Dump Debugging   1.0.9.31727
Enables debugging of Linux core dumps.

Microsoft Azure HDInsight Azure Node   2.6.1000.0
HDInsight Node under Azure Node

Microsoft Azure Hive Query Language Service   2.6.1000.0
Language service for Hive query

Microsoft Azure Service Fabric Tools for Visual Studio   16.10
Microsoft Azure Service Fabric Tools for Visual Studio

Microsoft Azure Stream Analytics Language Service   2.6.1000.0
Language service for Azure Stream Analytics

Microsoft Azure Stream Analytics Node   1.0
Azure Stream Analytics Node under Azure Node

Microsoft Azure Tools for Visual Studio   2.9
Support for Azure Cloud Services projects

Microsoft Continuous Delivery Tools for Visual Studio   0.4
Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE.

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft Library Manager   2.1.113+g422d40002e.RR
Install client-side libraries easily to any web project

Microsoft MI-Based Debugger   1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards   1.0
Microsoft Visual C++ Wizards

Microsoft Visual Studio Tools for Containers   1.2
Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Microsoft Visual Studio VC Package   1.0
Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio   16.10.15 (552afdf)
Support for debugging Mono processes with Visual Studio.

Node.js Tools   1.5.30526.3 Commit Hash:c09c81113bcbc86d57943fcdd67e82434263d61d
Adds support for developing and debugging Node.js apps in Visual Studio

NuGet Package Manager   5.11.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

Office Developer Tools for Visual Studio   16.0.31424.03
Microsoft Office Developer Tools for Visual Studio

ProjectServicesPackage Extension   1.0
ProjectServicesPackage Visual Studio Extension Detailed Info

Python   16.11.21196.2
Provides IntelliSense, projects, templates, debugging, interactive windows, and other support for Python developers.

Python - Conda support   16.11.21196.2
Conda support for Python projects.

Python - Django support   16.11.21196.2
Provides templates and integration for the Django web framework.

Python - Profiling support   16.11.21196.2
Profiling support for Python projects.

Razor (ASP.NET Core)   16.1.0.2122504+13c05c96ea6bdbe550bd88b0bf6cdddf8cde1725
Provides languages services for ASP.NET Core Razor.

SQL Server Data Tools   16.0.62107.28140
Microsoft SQL Server Data Tools

Test Adapter for Boost.Test   1.0
Enables Visual Studio's testing tools with unit tests written for Boost.Test.  The use terms and Third Party Notices are available in the extension installation directory.

Test Adapter for Google Test   1.0
Enables Visual Studio's testing tools with unit tests written for Google Test.  The use terms and Third Party Notices are available in the extension installation directory.

ToolWindowHostedEditor   1.0
Hosting json editor into a tool window

TypeScript Tools   16.0.30526.2002
TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools   3.11.0-4.21403.6+ae1fff344d46976624e68ae17164e0607ab68b10
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual C++ for Cross Platform Mobile Development (Android)   16.0.31506.176
Visual C++ for Cross Platform Mobile Development (Android)

Visual C++ for Linux Development   1.0.9.31727
Visual C++ for Linux Development

Visual F# Tools   16.11.0-beta.21322.6+488cc578cafcd261d90d748d8aaa7b8b091232dc
Microsoft Visual F# Tools

Visual Studio Code Debug Adapter Host Package   1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Container Tools Extensions   1.0
View, manage, and diagnose containers within Visual Studio.

Visual Studio Tools for CMake   1.0
Visual Studio Tools for CMake

Visual Studio Tools for Containers   1.0
Visual Studio Tools for Containers

Visual Studio Tools for Kubernetes   1.0
Visual Studio Tools for Kubernetes

Visual Studio Tools for Unity   4.11.4.0
Visual Studio Tools for Unity

VisualStudio.DeviceLog   1.0
Information about my package

VisualStudio.Foo   1.0
Information about my package

VisualStudio.Mac   1.0
Mac Extension for Visual Studio

VsVim   2.8.0.0
VsVim is a Vim emulator for Visual Studio

WiX Toolset Visual Studio Extension   1.0.0.4
WiX Toolset Visual Studio Extension version 1.0.0.4
Copyright (c) .NET Foundation and contributors. All rights reserved.

Workflow Manager Tools 1.0   1.0
This package contains the necessary Visual Studio integration components for Workflow Manager.

Xamarin   16.11.000.190 (d16-11@2391ed9)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer   16.11.0.17 (remotes/origin/11e0001f0b17269345e80b58fb3adf1ba4efe2cd@11e0001f0)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates   16.10.5 (355b57a)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK   11.4.0.5 (d16-11/7776c9f)
Xamarin.Android Reference Assemblies and MSBuild support.
    Mono: c633fe9
    Java.Interop: xamarin/java.interop/d16-11@48766c0
    ProGuard: Guardsquare/proguard/v7.0.1@912d149
    SQLite: xamarin/sqlite/3.35.4@85460d3
    Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-11@683f375


Xamarin.iOS and Xamarin.Mac SDK   15.0.0.8 (0796d78dc)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

Reproduction Link

https://github.com/jsclay/VSGMMemoryLeak.git

Workaround

While not yet attempted, the most likely workaround is using Triggers instead of VisualStateManger.

@jsclay jsclay added s/unverified New report that has yet to be verified t/bug 🐛 labels Nov 1, 2021
@jsuarezruiz jsuarezruiz added this to New in Triage via automation Nov 8, 2021
@jsuarezruiz jsuarezruiz moved this from New to Needs Estimate in Triage Nov 25, 2021
@jsuarezruiz jsuarezruiz removed the s/unverified New report that has yet to be verified label Nov 25, 2021
@8m0
Copy link

8m0 commented Oct 28, 2022

Wow, so another memory leak problem open from a year ago...

@pme442
Copy link

pme442 commented Mar 21, 2024

Any updates on this?

@pme442
Copy link

pme442 commented Mar 21, 2024

Has this bug been carried over to MAUI?

@MitchBomcanhao
Copy link

Has this bug been carried over to MAUI?

this one seems roughly in the same area, so I'd say it is very likely: dotnet/maui#21389

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Triage
  
Needs Estimate
Development

No branches or pull requests

6 participants