Skip to content

fix(controls): TitleBar throws NRE when Collapsed#1671

Open
luca-domenichini wants to merge 2 commits intolepoco:mainfrom
luca-domenichini:TitleBarNRE
Open

fix(controls): TitleBar throws NRE when Collapsed#1671
luca-domenichini wants to merge 2 commits intolepoco:mainfrom
luca-domenichini:TitleBarNRE

Conversation

@luca-domenichini
Copy link
Contributor

Pull request type

Please check the type of change your PR introduces:

  • Update
  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes

What is the current behavior?

Issue Number: #1670

What is the new behavior?

  • With this fix, TitleBar ignores non built buttons when hovering on owner Window

Other information

Copilot AI review requested due to automatic review settings March 6, 2026 22:21
@github-actions github-actions bot added controls Changes to the appearance or logic of custom controls. PR Pull request dotnet release titlebar Titlebar updates labels Mar 6, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a crash in Wpf.Ui’s TitleBar Win32 message hook path when the control is Visibility=Collapsed (and therefore never templated), preventing repeated NullReferenceExceptions during mouse hover.

Changes:

  • Adds a null-guard when iterating _buttons inside HwndSourceHook to avoid dereferencing template parts before OnApplyTemplate() runs.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines 677 to 681
foreach (TitleBarButton button in _buttons)
{
if (!button.ReactToHwndHook(message, lParam, out IntPtr returnIntPtr))
// Check if button is null to avoid potential NullReferenceException if OnApplyTemplate hasn't been called yet, e.g. when TitleBar has Visibility == Collapsed.
if (button == null || !button.ReactToHwndHook(message, lParam, out IntPtr returnIntPtr))
{
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

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

_buttons is declared as TitleBarButton[] but is initialized with null elements until OnApplyTemplate() runs. Adding button == null here fixes the immediate NRE, but it leaves nullability inconsistent and still assumes _buttons contains no nulls in the inner loop (e.g., anotherButton.IsHovered). Consider making _buttons a TitleBarButton?[] (or using an _isTemplateApplied flag / Array.Empty<TitleBarButton>() until templated) and iterating only over non-null buttons to keep the hook logic robust and aligned with nullable reference types.

Copilot uses AI. Check for mistakes.
Comment on lines +679 to 681
// Check if button is null to avoid potential NullReferenceException if OnApplyTemplate hasn't been called yet, e.g. when TitleBar has Visibility == Collapsed.
if (button == null || !button.ReactToHwndHook(message, lParam, out IntPtr returnIntPtr))
{
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

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

Please add a regression test for the reported scenario (TitleBar Visibility=Collapsed + mouse hover over the window) to ensure the window message hook no longer throws. There are existing FlaUI integration tests for TitleBar behavior, so this case should be covered to prevent reintroducing the NRE.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

controls Changes to the appearance or logic of custom controls. dotnet PR Pull request release titlebar Titlebar updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants