Skip to content

[Skia] The input manager keeps the element of the last manipulation alive after its pointer is released #25588

Closed

Description

@mikernet

Current behavior

On Skia, the managed InputManager.PointerManager keeps the gesture recognizers of the last manipulation alive after the pointer is released, and with them the element that started it.

RegisterUiElementManipulationRecognizer enqueues a UIElementRecognizer, which holds the UIElement and its GestureRecognizer, into a per pointer type queue in _gestureRecognizers (a PointerTypePseudoDictionary<Queue<IGestureRecognizer>>). On release or cancel, AfterReleaseForManipulations / AfterCancelForManipulations (and the redirected paths) call _gestureRecognizers.Remove(type). But Remove only clears the flag:

public void Remove(PointerDeviceType pointer)
	=> _hasValues[(int)pointer] = false;

The queue stays in _values[index], with its recognizers. It is only replaced when a later press on that pointer type starts another manipulation on a UIElement. Until then, the element that was last manipulated with the mouse (or pen, or touch) stays reachable from the InputManager, together with its visual tree and its DataContext, even after it has been removed from the tree and its page navigated away from.

Measured in a WebAssembly app (Skia renderer) with a GC heap dump (collectGcDump(), .NET 10). After scrolling a CommunityToolkit DataGrid with the mouse and navigating to another page, 80 MB of the 282 MB managed heap was reachable only through this path:

[strong handle] InputManager
  -> PointerManager
    -> PointerTypePseudoDictionary<Queue<IGestureRecognizer>>
      -> Queue<IGestureRecognizer>[]  (index 2, Mouse)
        -> Queue<IGestureRecognizer> -> IGestureRecognizer[]
          -> UIElementRecognizer
            -> DataGridRowsPresenter -> ... -> the departed page and its whole tree

DataGridRowsPresenter sets a manipulation mode, so a press on it registers a recognizer. The page stayed alive until another press started a manipulation somewhere else.

Expected behavior

Releasing or cancelling a pointer drops the references to the recognizers it used. Remove should also clear the stored queue:

public void Remove(PointerDeviceType pointer)
{
	var index = (int)pointer;
	_hasValues[index] = false;
	_values[index] = default!;
}

How to reproduce

  1. Add an element with ManipulationMode="All" (or any manipulation mode) to the window.
  2. Press and release the mouse on it.
  3. Remove the element from the tree and drop every reference to it. Move the mouse once, because PointerRoutedEventArgs.LastPointerEvent keeps the last event's source until the next pointer event. Collect and wait for pending finalizers.
  4. The element is still alive, held by InputManager.PointerManager._gestureRecognizers.

Environment

  • Uno.WinUI 6.8.0-dev.222 (Uno.Sdk 6.8.0-dev.66). The same code is on master at 55cddbc (src/Uno.UI/UI/Xaml/Internal/InputManager.Pointers.ManagedDirectManip.cs).
  • .NET 10, runtime pack 10.0.12.
  • Heads: Skia (the managed pointer pipeline): net10.0-browserwasm where it was measured, and net10.0-desktop.

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