Skip to content

[Skia] A long-lived ImageSource keeps every Image and ImageBrush that used it alive #25596

Description

@mikernet

Current behavior

An ImageSource that outlives the elements using it keeps every Image and ImageBrush that ever used it alive, along with everything they reference. Examples are an icon kept in a static field or in app resources, or a shared BitmapImage.

  • The source holds its subscribers strongly. ImageSource.crossruntime.cs keeps them in a strong List<Action<ImageData>>.
  • Image (Skia) never unsubscribes on unload. It subscribes with _sourceDisposable.Disposable = source.Subscribe(img => ...), and the callback captures the Image. It only unsubscribes when its Source changes. It has no unload handling.
  • 6.8 adds two more strong handlers for SVG sources. There, Image.skia.cs also subscribes to the SvgImageSource's SourceLoaded and OpenFailed events, which are released the same way.
  • ImageBrush is held twice. It subscribes to its source with _sourceSubscription, and it also adds ImageSource.Invalidated += brush.OnInvalidateRender. The brush in turn keeps the element it painted alive.

Measured in a WebAssembly app on 6.8 whose file type icons are SvgImageSources kept in static fields. After browsing documents for a while, they held 176 handlers and, through them, about 30 MB of pages the user had already left. A GC heap dump showed this chain:

static SvgImageSource -> OpenFailed -> Image -> binding -> template -> page

Expected behavior

A source does not keep its consumers alive. An Image or ImageBrush that is otherwise unreferenced is collected, whatever its source.

Status of the fixes

There is a separate path not covered here. A shareable object keeps the first object it's set on as its inheritance-context parent (_associatedParent) until a second user appears or the property is cleared. So a source used by only one element at a time still keeps that element alive. #25102 addresses this on master.

How to reproduce

  1. Keep an SvgImageSource or BitmapImage in a static field, and load it (SetSourceAsync).
  2. Show it in two Images, then remove them from the tree.
  3. Collect and wait for pending finalizers.
  4. Both Images are still alive, held through the source's subscriber list (and, on 6.8 for SVG, through its SourceLoaded and OpenFailed events). Using two Images sidesteps the _associatedParent path above.

Environment

  • Uno.WinUI 6.8.0-dev.222 (servicing/6.8 at 5efcb98), and master at 55cddbc.
  • Heads: Skia, measured on net10.0-browserwasm.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions