XBindLeakFix 1.0.0

dotnet add package XBindLeakFix --version 1.0.0
                    
NuGet\Install-Package XBindLeakFix -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="XBindLeakFix" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="XBindLeakFix" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="XBindLeakFix" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add XBindLeakFix --version 1.0.0
                    
#r "nuget: XBindLeakFix, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package XBindLeakFix@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=XBindLeakFix&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=XBindLeakFix&version=1.0.0
                    
Install as a Cake Tool

XBindLeakFix

NuGet

Automatic build-time fix for the WinUI 3 x:Bind memory leak.

Every WinUI 3 Window that uses x:Bind leaks memory because the XAML compiler generates an Activated event subscription that is never cleaned up. Since Window is not a DependencyObject, the XAML Reference Tracker can't break the resulting circular reference. This bug has been open since June 2022.

This package patches the generated code at build time. No code changes needed.

Install

dotnet add package XBindLeakFix

Or in your .csproj:

<PackageReference Include="XBindLeakFix" Version="1.0.0" />

How it works

On every build:

  1. XAML compiler generates .g.cs files (with the bug)
  2. XBindLeakFix patches them (injects cleanup on Window.Closed)
  3. C# compiler compiles the patched code

Only Window types using x:Bind are patched. Page, UserControl, and other FrameworkElement types are unaffected.

Results

BEFORE:  x:Bind windows created=5, finalized=0, leaked=5
AFTER:   x:Bind windows created=5, finalized=5, leaked=0

Root cause

The XAML compiler's T4 template (CSharpPagePass2.tt) generates:

element1.Activated += bindings.Activated;

but never generates the corresponding -=. For FrameworkElement types this is fine (Reference Tracker handles it). For Window (not a DependencyObject), the circular reference is permanent.

License

MIT

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 300 3/28/2026