-
Notifications
You must be signed in to change notification settings - Fork 992
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
base: main
Are you sure you want to change the base?
Fix #12661 Null Reference Exception: System.Windows.Forms.TabControl.<WmSelChange> #12683
Conversation
Codecov ReportAttention: Patch coverage is
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
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))); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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);
}
}));
There was a problem hiding this comment.
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.
src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/TabControlTests.cs
Outdated
Show resolved
Hide resolved
Application.DoEvents(); | ||
Thread.Sleep(1000); | ||
|
||
act.Should().NotThrow(); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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();
Fixes #12661
Proposed changes
Customer Impact
Regression?
Risk
Screenshots
Before
Throw exception in Application.Run()
After
No exception in runtime
Test methodology
Test environment(s)
Microsoft Reviewers: Open in CodeFlow