The Wayback Machine - https://web.archive.org/web/20260312142117/https://github.com/microsoft/terminal/pull/19950
Skip to content

Fix memory leaks with UIA#19950

Merged
carlos-zamora merged 5 commits intomainfrom
dev/cazamor/uia-leak-2
Mar 11, 2026
Merged

Fix memory leaks with UIA#19950
carlos-zamora merged 5 commits intomainfrom
dev/cazamor/uia-leak-2

Conversation

@carlos-zamora
Copy link
Member

Summary of the Pull Request

This includes the memory leak fixes that @lhecker and I investigated as a part of #19710.

The ITextRangeProviders (namely UiaTextRanges) weren't being destroyed after they were done being used by the screen reader.

Validation Steps Performed

In my own testing, I set a breakpoint on the destructor for UiaTextRangeBase. Prior to this change, that destructor would mainly be called when the terminal control was closed, which would result in us leaking these objects. With this change, I've confirmed that these text ranges are being destroyed immediately after they are done being used (without needing to close the terminal control).

PR Checklist

Closes #19710

@carlos-zamora carlos-zamora requested a review from lhecker March 6, 2026 03:17
@github-actions

This comment has been minimized.

Copy link
Member Author

@carlos-zamora carlos-zamora left a comment

Choose a reason for hiding this comment

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

@lhecker Could you do me a favor and double check that this actually fixes the leak?

We have notes from our initial investigation here: #19710 (comment)

I swear we found that there was still an issue with XAML, but I set a breakpoint on the dtor for InteractivityAutomationPeer, TermControlAutomationPeer, XamlUiaTextRange, and UiaTextRangeBase and they all seem to be getting hit properly now. I even checked them without the change and I've confirmed that the UiaTextRangeBase objects weren't getting dtor'd until the term control was closed. Am I going crazy? 🙃

{
[default_interface] runtimeclass InteractivityAutomationPeer :
Windows.UI.Xaml.Automation.Peers.AutomationPeer,
Windows.UI.Xaml.Automation.Peers.FrameworkElementAutomationPeer,
Copy link
Member Author

Choose a reason for hiding this comment

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

@lhecker
Copy link
Member

lhecker commented Mar 9, 2026

Ah fuck, I forgot that I promised to send a PR for this. ☹️ The remaining leak is inside XAML itself (see "Investigating Windows Terminal UIA Memory Leak").

Copy link
Member

@lhecker lhecker left a comment

Choose a reason for hiding this comment

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

Yep, that all looks correct to me!

@carlos-zamora carlos-zamora marked this pull request as ready for review March 9, 2026 21:48
@carlos-zamora carlos-zamora enabled auto-merge (squash) March 9, 2026 21:48
@github-project-automation github-project-automation bot moved this to To Cherry Pick in 1.23 Servicing Pipeline Mar 9, 2026
@github-project-automation github-project-automation bot moved this to To Cherry Pick in 1.24 Servicing Pipeline Mar 9, 2026
@carlos-zamora carlos-zamora moved this from To Cherry Pick to To Consider in 1.23 Servicing Pipeline Mar 9, 2026
@carlos-zamora carlos-zamora moved this from To Cherry Pick to To Consider in 1.24 Servicing Pipeline Mar 9, 2026
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment was marked as resolved.


void SetControlBounds(Windows.Foundation.Rect bounds);
void SetControlPadding(Microsoft.Terminal.Core.Padding padding);
void ParentProvider(Windows.UI.Xaml.Automation.Peers.AutomationPeer parentProvider);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we also use FrameworkElementAutomationPeer here (I'm not quite sure about this, I just made a short overview of the PR and found this other place including AutomationPeer and just thought that it's probably the best and quickest to ask)? And thanks for the nice PR!

Suggested change
void ParentProvider(Windows.UI.Xaml.Automation.Peers.FrameworkElementAutomationPeer parentProvider);

Copy link
Member Author

Choose a reason for hiding this comment

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

Good question! I think we're fine as is. It's currently implemented like this:

void InteractivityAutomationPeer::ParentProvider(AutomationPeer parentProvider)
{
// LOAD-BEARING: use _parentProvider->ProviderFromPeer(_parentProvider) instead of this->ProviderFromPeer(*this).
// Since we split the automation peer into TermControlAutomationPeer and InteractivityAutomationPeer,
// using "this" returns null. This can cause issues with some UIA Client scenarios like any navigation in Narrator.
_parentProvider = parentProvider ? parentProvider.as<IAutomationPeerProtected>().ProviderFromPeer(parentProvider) : nullptr;
}

With ProviderFromPeer() returning an IRawElementProviderSimple directly (docs link).

In practice, we're only ever passing in a FrameworkElementAutomationPeer, but really we're just using it to call the ProviderFromPeer() function on it. I'd rather keep it more flexible than less, unless there's a separate reason (which I'm open to).

@carlos-zamora carlos-zamora merged commit e2110e7 into main Mar 11, 2026
20 checks passed
@carlos-zamora carlos-zamora deleted the dev/cazamor/uia-leak-2 branch March 11, 2026 01:08
@github-project-automation github-project-automation bot moved this to To Cherry Pick in 1.25 Servicing Pipeline Mar 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To Consider
Status: To Cherry Pick

Development

Successfully merging this pull request may close these issues.

WT UI Automation memory leak

3 participants