[PR #3835] Fixed self reference capture in Tab and TerminalPage #25535

Open
opened 2026-01-31 09:10:08 +00:00 by claunia · 0 comments
Owner

Original Pull Request: https://github.com/microsoft/terminal/pull/3835

State: closed
Merged: Yes


Summary of the Pull Request

Every lambda capture in Tab and TerminalPage has been changed from capturing raw this to std::weak_ptr<Tab> or winrt::weak_ref<TerminalPage>. Lambda bodies have been changed to check the weak reference before use.

Capturing raw this in Tab's title change event handler was the root cause of #3776, and is fixed in this PR among other instance of raw this capture.

The lambda fixes to TerminalPage are unrelated to the core issue addressed in the PR checklist. Because I was already editing TerminalPage, figured I'd do a weak_ref pass.

References

PR Checklist

  • Closes #3776, potentially #2248, likely closes others
  • CLA signed. If not, go over here and sign the CLA
  • Tests added/passed
  • Requires documentation to be updated
  • I've discussed this with core contributors already. If not checked, I'm ready to accept this work might be rejected in favor of a different grand plan. Issue number where discussion took place: #3776

Detailed Description of the Pull Request / Additional comments

Tab now inherits from enable_shared_from_this, which enable accessing Tab objects as std::weak_ptr<Tab> objects. All instances of lambdas capturing this now capture std::weak_ptr<Tab> instead. TerminalPage is a WinRT type which supports winrt::weak_ref<TerminalPage>. All previous instance of TerminalPage lambdas capturing this has been replaced to capture winrt::weak_ref<TerminalPage>. These weak pointers/references can only be created after object construction necessitating for Tab a new function called after construction to bind lambdas.

Any anomalous crash related to the following functionality during closing a tab or WT may be fixed by this PR:

  • Tab icon updating
  • Tab text updating
  • Tab dragging
  • Clicking new tab button
  • Changing active pane
  • Closing an active tab
  • Clicking on a tab
  • Creating the new tab flyout menu

Sorry about all the commits. Will fix my fork after this PR! 😅

Validation Steps Performed

Attempted to repro the steps indicated in issue #3776 with the new changes and failed. When before the changes, the issue could consistently be reproed.

**Original Pull Request:** https://github.com/microsoft/terminal/pull/3835 **State:** closed **Merged:** Yes --- <!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request Every lambda capture in `Tab` and `TerminalPage` has been changed from capturing raw `this` to `std::weak_ptr<Tab>` or `winrt::weak_ref<TerminalPage>`. Lambda bodies have been changed to check the weak reference before use. Capturing raw `this` in `Tab`'s [title change event handler](https://github.com/microsoft/terminal/blob/master/src/cascadia/TerminalApp/Tab.cpp#L299) was the root cause of #3776, and is fixed in this PR among other instance of raw `this` capture. The lambda fixes to `TerminalPage` are unrelated to the core issue addressed in the PR checklist. Because I was already editing `TerminalPage`, figured I'd do a [weak_ref pass](https://github.com/microsoft/terminal/issues/3776#issuecomment-560575575). <!-- Other than the issue solved, is this relevant to any other issues/existing PRs? --> ## References <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist * [x] Closes #3776, potentially #2248, likely closes others * [x] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA * [ ] Tests added/passed * [ ] Requires documentation to be updated * [x] I've discussed this with core contributors already. If not checked, I'm ready to accept this work might be rejected in favor of a different grand plan. Issue number where discussion took place: #3776 <!-- Provide a more detailed description of the PR, other things fixed or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments `Tab` now inherits from `enable_shared_from_this`, which enable accessing `Tab` objects as `std::weak_ptr<Tab>` objects. All instances of lambdas capturing `this` now capture `std::weak_ptr<Tab>` instead. `TerminalPage` is a WinRT type which supports `winrt::weak_ref<TerminalPage>`. All previous instance of `TerminalPage` lambdas capturing `this` has been replaced to capture `winrt::weak_ref<TerminalPage>`. These weak pointers/references can only be created after object construction necessitating for `Tab` a new function called after construction to bind lambdas. Any anomalous crash related to the following functionality during closing a tab or WT may be fixed by this PR: - Tab icon updating - Tab text updating - Tab dragging - Clicking new tab button - Changing active pane - Closing an active tab - Clicking on a tab - Creating the new tab flyout menu Sorry about all the commits. Will fix my fork after this PR! 😅 <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed Attempted to repro the steps indicated in issue #3776 with the new changes and failed. When before the changes, the issue could consistently be reproed.
claunia added the pull-request label 2026-01-31 09:10:08 +00:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#25535