Skip to content

Exception when closing page with selected cell in tv:TableView (WinUI.TableView) #247

@MasterProger

Description

@MasterProger

Description:

Subject: Exception when closing page with selected cell in tv:TableView (WinUI.TableView)

Hello,

I encountered a reproducible crash in your tv:TableView control when a cell is selected and the host page/window is closed or unloaded. Short summary: the control continues input/hittest processing after the XAML visual tree is detached, which triggers WinRT/COM errors (RO_E_CLOSED) and a VisualTreeHelper error in DesktopWindowXamlSource mode.

Relevant native stack (summary)

  • KernelBase.dll → combase.dll → Microsoft.UI.Input.dll → (control code) → call into VisualTreeHelper / FindElementsInHostCoordinates.

What I believe is happening

  • The control performs pointer/hit-test operations (calls into VisualTreeHelper::FindElementsInHostCoordinates or similar) while the XAML visual tree is already being torn down or when no valid UIElement root is provided for DesktopWindowXamlSource. This causes invalid parameter errors (0x80070057) or access to already-closed WinRT objects (0x80000013). The root cause is a lifecycle race: input processing or asynchronous handlers outlive the visual tree.

Suggested fixes

  1. On control unload/dispose:
    • Stop/ cancel all async work and timers.
    • Unregister input/event handlers (PointerPressed/PointerMoved/Focus etc.).
    • Release pointer captures (ReleasePointerCapture / ReleasePointerCaptures) and clear selection.
    • Clear ItemsSource, SelectedItem/SelectedCells and avoid further access to detached UI elements.
  2. Before calling FindElementsInHostCoordinates, ensure a valid UIElement is passed when running under DesktopWindowXamlSource; guard against null / detached roots.
  3. Add and expose a public cleanup API (e.g., Detach, Dispose, StopInputProcessing) so hosts can deterministically tear down the control.
  4. Gracefully handle RO_E_CLOSED and related COM exceptions inside the control to avoid crashing the host.
  5. Consider adding defensive synchronization to avoid asynchronous continuations touching the control after Unloaded.

What I can provide on request

  • Managed callstack (stack frames from the .NET side) at the moment of the exception.
  • Minimal repro project and the exact XAML + page code.
  • The specific NuGet package id and version used in my project (if you request it).

Please let me know which data you need and I will attach the managed stack and a small repro. Thank you.

Steps to Reproduce:

Reproduction steps (minimal)

  1. Open the page containing tv:TableView.
  2. Select any cell.
  3. Close the page / navigate away / close the app so the page is unloaded.
  4. Observe crash/exception (first-chance or fatal).

Expected behavior:

Observed exceptions

  • RO_E_CLOSED (0x80000013): "The given object has already been closed / disposed and may no longer be used."
  • 0x80070057 from VisualTreeHelper::FindElementsInHostCoordinates:
    "FindElementsInHostCoordinates failed because the XAML runtime is running to support DesktopWindowXamlSource, and in this mode a UIElement must be provided."

Screenshots:

Image Image Image

Environment:

Environment

  • WinUI 3 (Desktop) app hosted with DesktopWindowXamlSource
  • .NET 10, Visual Studio 2022
  • tv:TableView used in a Page (Pages/MainPage.xaml) with SelectionMode="Single" and SelectionUnit="Cell".
  • ItemsSource bound via {x:Bind Documents}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions