Skip to content

Fix ArgumentNullException in chrome hit test after window close - #22409

Merged
MrJul merged 2 commits into
AvaloniaUI:mainfrom
AtakanUk:fix-chrome-hit-test-after-close
Oct 8, 2026
Merged

MrJul merged 2 commits into
AvaloniaUI:mainfrom
AtakanUk:fix-chrome-hit-test-after-close

Conversation

@AtakanUk

@AtakanUk AtakanUk commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

What does the pull request do?

Makes PresentationSource.HitTestChromeElement return null once RootVisual has been cleared by TopLevel.HandleClosed().

What is the current behavior?

A closed window with an extended client area can still receive WM_NCHITTEST and other non-client messages. WindowImpl.HitTestVisual passes them to HitTestChromeElement, which dereferences the null RootVisual and throws ArgumentNullException inside the window procedure, so the process terminates.

What is the updated/expected behavior with this PR?

A closed window reports no chrome element and the non-client hit test falls back to the regular result.

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

The check is in PresentationSource rather than in the Win32 WindowImpl, so the X11 and Wayland callers are covered too. Both already treat null as "no chrome element".

I ran the repro from the issue on Windows 11 with each WindowDecorations value, closing the window both with Close() and with SC_CLOSE. Before the change every window with an extended client area crashed the process (0xC000041D); after it the non-client messages are handled normally. Hit-test results on open windows are the same before and after.

Checklist

Breaking changes

None.

Fixed issues

Fixes #22324

@MrJul

MrJul commented Oct 8, 2026

Copy link
Copy Markdown
Member

@cla-avalonia come on, say something

@cla-avalonia

cla-avalonia commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator
  • All contributors have signed the CLA.

@AtakanUk

AtakanUk commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

@cla-avalonia agree

@MrJul MrJul added bug os-windows backport-candidate-12.1.x Consider this PR for backporting to 12.1 branch labels Oct 8, 2026
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown

NuGet packages

The packages for this pull request have been published (workflow run):

@MrJul

MrJul commented Oct 8, 2026

Copy link
Copy Markdown
Member

RootVisual should really be nullable (as noted in the code comments).
It's not a trivial change, so I opened #22411 to track it.

Meanwhile, this PR is a good fix.

@MrJul MrJul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@MrJul
MrJul added this pull request to the merge queue Oct 8, 2026
Merged via the queue into AvaloniaUI:main with commit 3b8d0fb Oct 8, 2026
12 checks passed
@AtakanUk
AtakanUk deleted the fix-chrome-hit-test-after-close branch October 8, 2026 19:21
MrJul pushed a commit that referenced this pull request Oct 9, 2026
* Add failing test for chrome hit test on a closed window

* Return null from chrome hit test once the root visual is cleared
@MrJul MrJul added backported-12.1.x and removed backport-candidate-12.1.x Consider this PR for backporting to 12.1 branch labels Oct 9, 2026
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.

Win32: WM_NCHITTEST on a closed window with extended client area throws ArgumentNullException (Parameter 'visual') from HitTestChromeElement

3 participants