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

Fix #12661 Null Reference Exception: System.Windows.Forms.TabControl.<WmSelChange> #12683

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

John-Qiao
Copy link
Member

@John-Qiao John-Qiao commented Dec 25, 2024

Fixes #12661

Proposed changes

  • Update the WmSelChange method in TabControl.cs to check if the AccessibilityObject is null before calling the method
  • Add a unit test to verify the fix

Customer Impact

  • Throw exception in Application.Run()

Regression?

  • No

Risk

  • Minimal

Screenshots

Before

Throw exception in Application.Run()
Actual

After

No exception in runtime

Test methodology

  • Manually
  • Unit test

Test environment(s)

  • 10.0.0-alpha.1.24622.1
Microsoft Reviewers: Open in CodeFlow

@John-Qiao John-Qiao requested a review from a team as a code owner December 25, 2024 10:44
@John-Qiao John-Qiao changed the title Fix issue 12661 and add unit test Fix #12661 Null Reference Exception: System.Windows.Forms.TabControl.<WmSelChange> Dec 25, 2024
Copy link

codecov bot commented Dec 25, 2024

Codecov Report

Attention: Patch coverage is 90.00000% with 4 lines in your changes missing coverage. Please review.

Project coverage is 76.03363%. Comparing base (084e6d0) to head (43f7faa).
Report is 1 commits behind head on main.

Additional details and impacted files
@@                 Coverage Diff                 @@
##                main      #12683         +/-   ##
===================================================
+ Coverage   76.03186%   76.03363%   +0.00177%     
===================================================
  Files           3181        3181                 
  Lines         639670      639709         +39     
  Branches       47215       47218          +3     
===================================================
+ Hits          486353      486394         +41     
+ Misses        149797      149791          -6     
- Partials        3520        3524          +4     
Flag Coverage Δ
Debug 76.03363% <90.00000%> (+0.00177%) ⬆️
integration 18.16430% <0.00000%> (-0.01198%) ⬇️
production 49.82409% <100.00000%> (+0.00210%) ⬆️
test 97.02835% <89.74359%> (-0.00080%) ⬇️
unit 47.05064% <100.00000%> (+0.00773%) ⬆️

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

@@ -1957,7 +1957,7 @@ private bool WmSelChange()
if (IsAccessibilityObjectCreated && SelectedTab?.ParentInternal is TabControl)
{
SelectedTab.TabAccessibilityObject.RaiseAutomationEvent(UIA_EVENT_ID.UIA_SelectionItem_ElementSelectedEventId);
BeginInvoke((MethodInvoker)(() => SelectedTab.TabAccessibilityObject.RaiseAutomationEvent(UIA_EVENT_ID.UIA_AutomationFocusChangedEventId)));
BeginInvoke((MethodInvoker)(() => SelectedTab?.TabAccessibilityObject.RaiseAutomationEvent(UIA_EVENT_ID.UIA_AutomationFocusChangedEventId)));
Copy link
Member

Choose a reason for hiding this comment

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

We should also test if (IsAccessibilityObjectCreated && SelectedTab?.ParentInternal is TabControl) inside the method invoker delegate in case the control had been disposed before the delegate was invoked.

Copy link
Member

Choose a reason for hiding this comment

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

In the BeginInvoke delegate, should we consider adding conditional checks for IsAccessibilityObjectCreated and SelectedTab?.ParentInternal is TabControl to ensure the state is valid at execution time?

BeginInvoke((MethodInvoker)(() =>
{
     if (IsAccessibilityObjectCreated && SelectedTab?.ParentInternal is TabControl && 
          !SelectedTab.IsDisposed && SelectedTab.TabAccessibilityObject != null)
     {
        SelectedTab.TabAccessibilityObject.RaiseAutomationEvent(UIA_EVENT_ID.UIA_AutomationFocusChangedEventId);
     }
}));

Copy link
Member

@Tanya-Solyanik Tanya-Solyanik Dec 26, 2024

Choose a reason for hiding this comment

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

Is SelectedTab.IsDisposed possible? Please double-check if we are un-selecting tabs before disposing them.

It's ok to create accessibility object for a non-disposed child control, if the parent has an accessible object. The child accessible object will be created to support some other user action anyway if accessibility tools are being used. And we know that accessibility tools are used because they initiated creation of the parent accessible object.

Application.DoEvents();
Thread.Sleep(1000);

act.Should().NotThrow();
Copy link
Member

Choose a reason for hiding this comment

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

In this scenario, exception happens after WmSelChange had been executed, the delegate is only scheduled for execution with BeginInvoke.

    control.TestAccessor().Dynamic.WmSelChange();
    Application.DoEvents();
    Thread.Sleep(100);
   // no exception

Copy link
Member

Choose a reason for hiding this comment

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

So here we need to ensure that SelectedTab is diaposed after WmSelChange is executed and before the event UIA_AutomationFocusChangedEventId is executed.

 Action act = () => control.TestAccessor().Dynamic.WmSelChange();
 act.Should().NotThrow();

 control.TabPages.Clear();

 var exception = Record.Exception(() =>
 {
     Application.DoEvents();
     Thread.Sleep(100);
 });

 exception.Should().BeNull();

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.

Null Reference Exception: System.Windows.Forms.TabControl.<WmSelChange>
3 participants