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

Avoid NullReferenceException in Gestures.PointerReleased #15117

Merged

Conversation

zabolotnev
Copy link
Contributor

What does the pull request do?

PR allows you to avoid crashes during multi-touch input #14249. This is not a complete fix as I'm not sure the new behavior is correct.

What is the current behavior?

The bug is described in #14249. In my case, I created an application with one large button. I pressed the Linux device's touch display several times with three fingers. After a few touches the app crashes with message:

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at Avalonia.Input.Gestures.PointerReleased(RoutedEventArgs ev)

What is the updated/expected behavior with this PR?

The application does not crash.

How was the solution implemented (if it's not obvious)?

There is three ways to avoid NRE here:

  1. Add a null check in PointerReleased before raising the event. In this case the HoldingState.Completed event will not be raised in some cases. Is it good or bad? I don't know.
  2. Store the value of s_lastHeldPointer.Type in separate field. Event HoldingState.Completed will be raised even if s_lastHeldPointer is null. I think this is better than the first case.
  3. Figure out why s_lastHeldPointer might be null and causes NRE and rewrite (refactor?) this code.

I decided to take the second path as it is more reliable. I also changed the PointerPressed and PointerMoved methods to use s_lastHeldPointerType field just for consistency. These two methods do not throw NRE exceptions.

Checklist

Breaking changes

Obsoletions / Deprecations

Fixed issues

@zabolotnev
Copy link
Contributor Author

@dotnet-policy-service agree

@zabolotnev zabolotnev force-pushed the fix/NRE_in_Gestures_PointerReleased branch from 827419d to 0c965be Compare March 25, 2024 11:54
@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.2.999-cibuild0046577-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.2.999-cibuild0046793-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@maxkatz6 maxkatz6 merged commit 04c6285 into AvaloniaUI:master Apr 5, 2024
10 checks passed
@maxkatz6 maxkatz6 added the backport-candidate-11.1.x Consider this PR for backporting to 11.1 branch label Apr 5, 2024
maxkatz6 pushed a commit that referenced this pull request Apr 6, 2024
* refs #14249 Avoid NullReferenceException in Gestures.PointerReleased

* refs #14249 Track current gesture state with single readonly struct

* Update from review

* Moved the last click location from the GestureState record to a separate field. (#14249)

---------

Co-authored-by: Заболотнев Юрий <zabolotnev@promit-ek.ru>
@zabolotnev zabolotnev deleted the fix/NRE_in_Gestures_PointerReleased branch April 8, 2024 08:05
@maxkatz6 maxkatz6 added backported-11.1.x and removed backport-candidate-11.1.x Consider this PR for backporting to 11.1 branch labels Apr 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants