Skip to content

Fix ComboBox crash during handle creation with autocomplete on non-system-DPI monitors - #15298

Merged
LeafShi1 merged 1 commit into
dotnet:mainfrom
LeafShi1:Fix_15286_ComboBox_crash_during_handle_creation
Oct 23, 2026
Merged

LeafShi1 merged 1 commit into
dotnet:mainfrom
LeafShi1:Fix_15286_ComboBox_crash_during_handle_creation

Conversation

@LeafShi1

@LeafShi1 LeafShi1 commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Fixes #15286

Root cause

Starting with .NET 7, per-monitor V2 DPI handling can scale an explicitly assigned font during Control.OnHandleCreated, synchronously invoking ComboBox.OnFontChanged.

When AutoCompleteMode is not None, ComboBox.OnFontChanged unconditionally recreates the handle. This destroys the handle while its initial creation is still in progress. The nested creation attempt returns early because States.CreatingHandle is set, leaving the remaining ComboBox initialization without a valid handle.

.NET 6 has the same autocomplete-related handle recreation logic, but its handle-creation DPI path does not perform the font scaling that triggers this reentrancy.

Proposed changes

  • Skip autocomplete-related handle recreation in ComboBox.OnFontChanged while States.CreatingHandle is set. Autocomplete initialization proceeds afterward as part of the existing handle-creation flow.
  • Preserve handle recreation for font changes after handle creation has completed.
  • Add regression tests covering DPI-related font changes during handle creation and normal font changes afterward, across all autocomplete modes.

Customer Impact

  • Prevents applications from crashing when adding a ComboBox with an explicitly assigned font and a non-None autocomplete mode on a monitor whose DPI differs from the system DPI.

Regression?

  • Yes

Risk

  • Minimal

Screenshots

Before

Run the attached application, on a non-system-DPI monitor, click the button to reproduce the crash.
TestComboBox.zip

devenv_BJDIKfNUlw.mp4

After

AfterChanges.mp4

Test methodology

  • Unit test and manually

Test environment(s)

  • 11.0.0-rc.2.26509.113
Microsoft Reviewers: Open in CodeFlow

@LeafShi1
LeafShi1 requested a review from a team as a code owner October 22, 2026 03:33
@codecov

codecov Bot commented Oct 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 37.84861%. Comparing base (c4891de) to head (8f5411b).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@              Coverage Diff              @@
##                main      #15298   +/-   ##
=============================================
  Coverage   37.84861%   37.84861%           
=============================================
  Files            246         246           
  Lines           9789        9789           
  Branches        1031        1031           
=============================================
  Hits            3705        3705           
  Misses          5920        5920           
  Partials         164         164           
Flag Coverage Δ
Debug 37.84861% <ø> (ø)
production 40.04608% <ø> (ø)
test 20.64923% <ø> (ø)
unit 40.04608% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The narrow lifecycle guard addresses the reported crash while focused regression tests cover both the corrected and preserved behavior.

No open issues 🎉

What changed in this PR

Fixes #15286 by preventing ComboBox handle destruction when DPI scaling changes an explicitly assigned font during handle creation.

Changes:

  • Skip autocomplete-related handle recreation while handle creation is in progress.
  • Preserve handle recreation for subsequent font changes.
  • Add regression tests across all autocomplete modes, checking handle lifecycle and retained item functionality.
File Description
src/​test/​unit/​System.Windows.Forms/​System/​Windows/​Forms/​ComboBoxTests.cs Tests font changes during and after handle creation across autocomplete modes.
src/​System.Windows.Forms/​System/​Windows/​Forms/​Controls/​ComboBox/​ComboBox.cs Guards autocomplete-related handle recreation with the handle-creation state.

Review effort: Balanced

@SimonZhao888 SimonZhao888 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.

Looks good to me!

@LeafShi1
LeafShi1 merged commit b6221fe into dotnet:main Oct 23, 2026
10 checks passed
@LeafShi1
LeafShi1 deleted the Fix_15286_ComboBox_crash_during_handle_creation branch October 23, 2026 01:15
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enable Per-Monitor DPI Awareness, crashes when add ComboBox to a monitor with a DPI different from the system DPI.

4 participants