Skip to content

PdfView stays on TopLevel.ScalingChanged after it leaves the visual tree, so a closed viewer and its document are never collected #599

Open

Description

@AntonBaluev

Version: Broadside.Viewer.Avalonia 0.1.0-preview.10, Avalonia 12.1.6 (the package pins 12.1.4), .NET 10, Windows; seen headless (Avalonia.Headless) and by reading the code.

What happens. PdfView.OnAttachedToVisualTree subscribes topLevel.ScalingChanged += OnScalingChanged. OnDetachedFromVisualTree unsubscribes through TopLevel.GetTopLevel(this), which is already null when Avalonia calls it (the visual root is cleared before the override runs), so the -= never happens. Every PdfView that was ever shown in a window stays referenced by that window's ScalingChanged delegate, and through the view its controller, its last session's caches and whatever the host hung on it. Disposing the view does not help: Dispose does not unsubscribe either.

Repro (a window that outlives the viewer, as a tabbed app's main window does):

var host = new ContentControl();
var window = new Window { Content = host, Width = 800, Height = 600 };
window.Show();
var views = new List<WeakReference>();
for (var i = 0; i < 6; i++)
{
    var session = DocumentSession.Open(bytes);          // any PDF
    var view = new PdfView { Session = session };
    host.Content = view;
    // ... wait until view.State is { PageCount: > 0, IsRendering: false, IsLoading: false }
    host.Content = null;
    view.Session = null;
    session.Dispose();
    view.Dispose();
    views.Add(new WeakReference(view));
}
GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect();
// Expected: 0 alive. Actual: 6 alive.
// GC root (ClrMD): Window -> EventHandler (ScalingChanged invocation list) -> PdfView.

Closing the window releases them, which is why a test that closes its window does not see it.

Suggested fix. In OnDetachedFromVisualTree, unsubscribe from e.Root as TopLevel (the root it is leaving) instead of TopLevel.GetTopLevel(this); and unsubscribe in Dispose too.

Workaround we ship meanwhile: on the view's DetachedFromVisualTree event and on dispose, remove OnScalingChanged from (e.Root as TopLevel).ScalingChanged by reflection. Removing a handler that is not there does nothing, so it stays harmless once this is fixed.

Still present on main: src/Broadside.Viewer.Avalonia/PdfView.cs, OnDetachedFromVisualTree (around line 330) and Dispose (around line 268).

Activity

  1. added this to the Phase 6: Viewer milestone on Nov 24, 2026
  2. michaelstonis commented on Nov 24, 2026

    @michaelstonis
    Owner

    Viewer track: the viewer packages are not part of v1.0 (owner decision on #276), so this does not block 1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions