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 crash in NSLayoutManager during autorelease dealloc #523

Merged
merged 52 commits into from
Jul 28, 2020

Conversation

tom-un
Copy link
Collaborator

@tom-un tom-un commented Jul 28, 2020

Please select one of the following

  • I am removing an existing difference between facebook/react-native and microsoft/react-native-macos 👍
  • I am cherry-picking a change from Facebook's react-native into microsoft/react-native-macos 👍
  • I am making a fix / change for the macOS implementation of react-native
  • I am making a change required for Microsoft usage of react-native

Summary

The RCTTextView has an NSTextStorage ivar. When these objects are released and dealloced in an autorelease pool, sometimes there can be an exception thrown in system code as NSLayoutManager dealloc calls its own _NSRemoveDirtyLayoutManager method which fails on a call to -[NSConcretePointerArray removePointerAtIndex:].

We used to hit this issue consistently when resizing a window that contained many runs of text. The workaround was to manually remove all layout managers from the _textStorage ivar just prior to replacing it with another NSTextStorage.

Even with this fix, the [RNTesterSnapshotTests testTextExample] test would still occasionally crash with the same callstack, and now @ospfranco reported the crash in a dependent project. Doing the same operation of manually removing all the layout managers from the text storage in RCTTextView dealloc seems to fix the issue. #357

Changelog

[fixed] [macOS] - Fix crash in NSLayoutManager during autorelease dealloc

Test Plan

Re-enabled the previously disabled [RNTesterSnapshotTests testTextExample] test. @ospfranco manually tested the change.

Microsoft Reviewers: Open in CodeFlow

tom-un and others added 30 commits April 3, 2020 20:53
@tom-un tom-un requested a review from HeyImChris July 28, 2020 18:12
@pull-bot
Copy link

pull-bot commented Jul 28, 2020

Messages
📖

📋 Verify Changelog Format - A changelog entry has the following format: [CATEGORY] [TYPE] - Message.

CATEGORY may be:
  • General
  • macOS
  • iOS
  • Android
  • JavaScript
  • Internal (for changes that do not need to be called out in the release notes)

TYPE may be:

  • Added, for new features.
  • Changed, for changes in existing functionality.
  • Deprecated, for soon-to-be removed features.
  • Removed, for now removed features.
  • Fixed, for any bug fixes.
  • Security, in case of vulnerabilities.

MESSAGE may answer "what and why" on a feature level. Use this to briefly tell React Native users about notable changes.

Generated by 🚫 dangerJS against 55bd800

// associated NSLayoutManager dealloc later in an autorelease pool.
// Manually removing the layout managers from _textStorage prior to release
// works around this issue in AppKit.
NSArray<NSLayoutManager *> *managers = [_textStorage layoutManagers];

Choose a reason for hiding this comment

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

Can we confirm this makes a copy so we don't crash on mutating a collection while iterating it? If not we should explicitly copy the result of [_textStorage layoutManagers]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Its a good call to copy it. In practice there's only ever a single object in the array but that could change.

Copy link

@HeyImChris HeyImChris left a comment

Choose a reason for hiding this comment

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

Looks like we're hitting the brew bundle CI validation failure here too

@tom-un tom-un merged commit 40c5a66 into microsoft:master Jul 28, 2020
christophpurrer pushed a commit to christophpurrer/react-native-macos that referenced this pull request Jan 23, 2022
Summary: In a prior diff we removed some code that fixed an occasional crash in RCTTextView. This diff brings back the code, adapted from microsoft#523.

Test Plan:
Open the Text section of RNTester several times and verify no crash:
|Before|After|
|{F612971452}|{F612971479}|

Reviewers: skyle

Reviewed By: skyle

Subscribers: eliwhite

Differential Revision: https://phabricator.intern.facebook.com/D28166631

Tasks: T78743077

Signature: 28166631:1620086528:d359ab6e0c3477f2e52784534b6763156b33b1e5
christophpurrer pushed a commit to christophpurrer/react-native-macos that referenced this pull request Jan 30, 2022
Summary: In a prior diff we removed some code that fixed an occasional crash in RCTTextView. This diff brings back the code, adapted from microsoft#523.

Test Plan:
Open the Text section of RNTester several times and verify no crash:
|Before|After|
|{F612971452}|{F612971479}|

Reviewers: skyle

Reviewed By: skyle

Subscribers: eliwhite

Differential Revision: https://phabricator.intern.facebook.com/D28166631

Tasks: T78743077

Signature: 28166631:1620086528:d359ab6e0c3477f2e52784534b6763156b33b1e5
christophpurrer pushed a commit to christophpurrer/react-native-macos that referenced this pull request Mar 5, 2022
Summary: In a prior diff we removed some code that fixed an occasional crash in RCTTextView. This diff brings back the code, adapted from microsoft#523.

Test Plan:
Open the Text section of RNTester several times and verify no crash:
|Before|After|
|{F612971452}|{F612971479}|

Reviewers: skyle

Reviewed By: skyle

Subscribers: eliwhite

Differential Revision: https://phabricator.intern.facebook.com/D28166631

Tasks: T78743077

Signature: 28166631:1620086528:d359ab6e0c3477f2e52784534b6763156b33b1e5
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.

<TextView>: Intermittent crash in [NSTextStorage dealloc] during AutoRelease
5 participants