Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.NullReferenceException in Avalonia.Controls.Utils.KeyboardHelper #99

Open
scottdoingwork opened this issue Jul 2, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@scottdoingwork
Copy link

Describe the bug

var keymap = TopLevel.GetTopLevel(target)!.PlatformSettings!.HotkeyConfiguration;
TopLevel.GetTopLevel(target) returns null, causing program crash.

System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Avalonia.Controls.DataGrid
StackTrace:
at Avalonia.Controls.Utils.KeyboardHelper.GetPlatformCtrlOrCmdKeyModifier(Control target) in Avalonia.Controls.Utils\KeyboardHelper.cs:line 22
......
at Avalonia.Win32.WindowImpl in src\Windows\Avalonia.Win32\WindowImpl.AppWndProc.cs 769

To Reproduce

Put DataGrid in a popup. Pressing mouse left button on datagrid let popup close immediately.

Expected behavior

Program won't crash in this situation.

Avalonia version

11.0.11

OS

Windows

Additional context

No response

@scottdoingwork scottdoingwork added the bug Something isn't working label Jul 2, 2024
@MidnightEmpire
Copy link

Same issue... Maybe exist some workaround?

@timunie
Copy link
Contributor

timunie commented Jul 4, 2024

Best "workaround" would be to clone the source, try to solve the issue and file a PR.

@maxkatz6
Copy link
Member

maxkatz6 commented Jul 4, 2024

Yeah, it should be easy to add a null check.
Not necessary the best solution there, as PlatformSettings should not be null for popups.

@KittySey
Copy link

I encountered a similar issue. The problem is a race condition when the DataGrid is removed from the Visual Tree before input handling is completed.

To avoid this in your case, close the popup asynchronously on the UI thread. Otherwise, the DataGrid will be detached from the Visual Tree and its VisualRoot set to null before the method in KeyboardHelper can retrieve the VisualRoot element. Here's a simple example:

private void DataGrid_OnCellPointerPressed(object? sender, DataGridCellPointerPressedEventArgs e)
{
    Dispatcher.UIThread.Post(() => myPopup.IsOpen = false);
}

For more info about the UI thread, see: https://docs.avaloniaui.net/docs/guides/development-guides/accessing-the-ui-thread

@SiMet
Copy link

SiMet commented Dec 16, 2024

We observe same issue with version 11.1.3

Stack Trace
KeyboardHelper.cs in KeyModifiers KeyboardHelper.GetPlatformCtrlOrCmdKeyModifier(Control target) at line 28 
KeyboardHelper.cs in void KeyboardHelper.GetMetaKeyState(Control target, KeyModifiers modifiers, out bool ctrlOrCmd, out bool shift) at line 15 
DataGridColumnHeader.cs in void DataGridColumnHeader.ProcessSort(KeyModifiers keyModifiers, ListSortDirection? forcedDirection) at line 256 
DataGridColumnHeader.cs in void DataGridColumnHeader.InvokeProcessSort(KeyModifiers keyModifiers, ListSortDirection? forcedDirection)+() => { } [1] at line 226 
DispatcherOperation.cs in void DispatcherOperation.InvokeCore() at line 275 
DispatcherOperation.cs in void DispatcherOperation.Execute() at line 263 
Dispatcher.Queue.cs in void Dispatcher.ExecuteJob(DispatcherOperation job) at line 137 
Dispatcher.Queue.cs in void Dispatcher.ExecuteJobsCore(bool fromExplicitBackgroundProcessingCallback) at line 168 
Dispatcher.Timers.cs in void Dispatcher.OnOSTimer() at line 130 
Win32Platform.cs in IntPtr Win32Platform.WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam) at line 138 
IntPtr UnmanagedMethods.DispatchMessage(ref MSG lpmsg) 
Win32DispatcherImpl.cs in void Win32DispatcherImpl.RunLoop(CancellationToken cancellationToken) at line 111 
DispatcherFrame.cs in void DispatcherFrame.Run(IControlledDispatcherImpl impl) at line 121 
Dispatcher.MainLoop.cs in void Dispatcher.PushFrame(DispatcherFrame frame) at line 53 
Dispatcher.MainLoop.cs in void Dispatcher.MainLoop(CancellationToken cancellationToken) at line 80 
ClassicDesktopStyleApplicationLifetime.cs in int ClassicDesktopStyleApplicationLifetime.Start(string[] args) at line 130 
ClassicDesktopStyleApplicationLifetime.cs in int ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime(AppBuilder builder, string[] args, ShutdownMode shutdownMode) at line 278 
void Program.StartApp(string[] args) 
void Program.LunisolarCalendarRemoveEmptyEntries(Program, string[]) 
void Program.Main(string[] args)

@MrJul MrJul transferred this issue from AvaloniaUI/Avalonia Mar 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants