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

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

📋 Pull Request Information

Original PR: https://github.com/microsoft/terminal/pull/3835
Author: @mkitzan
Created: 12/4/2019
Status: Merged
Merged: 12/5/2019
Merged by: @undefined

Base: masterHead: fix-tab-reference-capture


📝 Commits (10+)

📊 Changes

3 files changed (+139 additions, -67 deletions)

View changed files

📝 src/cascadia/TerminalApp/Tab.cpp (+52 -27)
📝 src/cascadia/TerminalApp/Tab.h (+4 -1)
📝 src/cascadia/TerminalApp/TerminalPage.cpp (+83 -39)

📄 Description

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.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/microsoft/terminal/pull/3835 **Author:** [@mkitzan](https://github.com/mkitzan) **Created:** 12/4/2019 **Status:** ✅ Merged **Merged:** 12/5/2019 **Merged by:** [@undefined](undefined) **Base:** `master` ← **Head:** `fix-tab-reference-capture` --- ### 📝 Commits (10+) - [`d6c9d76`](https://github.com/microsoft/terminal/commit/d6c9d765bafe3076c48a0b411c8279aebda36b8e) Added FindGuid helper function - [`033861a`](https://github.com/microsoft/terminal/commit/033861a98aad3643ccfcf8ce4917d2eec47317b2) Style change - [`b4c5e4e`](https://github.com/microsoft/terminal/commit/b4c5e4e14fddff374610ce01cb8295d81281fe07) Tests for FindGuid and FindProfile - [`b4d57da`](https://github.com/microsoft/terminal/commit/b4d57da406328383d6e2e3eeb8995cf5816801cb) Fixed code format? - [`10d9578`](https://github.com/microsoft/terminal/commit/10d9578117f46ef820357672888564716c1f3bc4) Code format guess - [`08de164`](https://github.com/microsoft/terminal/commit/08de1643dc020c5f1983c8dc0146755c8ba0f543) optional<GUID> fix - [`48ff6cd`](https://github.com/microsoft/terminal/commit/48ff6cd3d5e47a4395e73ac9156119a84116d074) Updated function desc - [`d7cb7f4`](https://github.com/microsoft/terminal/commit/d7cb7f4126e3340dd67b37d59212b21e573edc16) Fixed throwing cast - [`afc8ed3`](https://github.com/microsoft/terminal/commit/afc8ed30f17b85afd90485bb4d4de043aedc0853) Merge branch 'find-guid-helper' - [`f7349d7`](https://github.com/microsoft/terminal/commit/f7349d79f51d94a0cccb26dcd4c9ea4a3aacdd31) Merge branch 'floating-point-comp' ### 📊 Changes **3 files changed** (+139 additions, -67 deletions) <details> <summary>View changed files</summary> 📝 `src/cascadia/TerminalApp/Tab.cpp` (+52 -27) 📝 `src/cascadia/TerminalApp/Tab.h` (+4 -1) 📝 `src/cascadia/TerminalApp/TerminalPage.cpp` (+83 -39) </details> ### 📄 Description <!-- 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-31 09:10:06 +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#25530