Skip to content

Commit 9ead4f9

Browse files
committed
Fix primaryTextLayoutManager memory issue and initialization order
Resolves an issue with the text layout manager by ensuring proper initialization order and preventing memory issues. The main changes include setting the primary text layout manager to nil before removing it, setting text content after setup, and reordering initialization steps to avoid potential crashes during view setup.
1 parent 83aa14f commit 9ead4f9

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

Sources/STTextViewAppKit/STTextView.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -403,12 +403,14 @@ import AVFoundation
403403
/// The manager that lays out text for the text view's text container.
404404
@objc dynamic open var textLayoutManager: NSTextLayoutManager {
405405
willSet {
406+
textContentManager.primaryTextLayoutManager = nil
406407
textContentManager.removeTextLayoutManager(newValue)
407408
}
408409
didSet {
409410
textContentManager.addTextLayoutManager(textLayoutManager)
410411
textContentManager.primaryTextLayoutManager = textLayoutManager
411412
setupTextLayoutManager(textLayoutManager)
413+
self.text = text
412414
}
413415
}
414416

@@ -612,8 +614,6 @@ import AVFoundation
612614

613615
super.init(frame: frameRect)
614616

615-
setupTextLayoutManager(textLayoutManager)
616-
617617
textFinderBarContainer.client = self
618618
textFinder.findBarContainer = textFinderBarContainer
619619

@@ -636,6 +636,7 @@ import AVFoundation
636636
addGestureRecognizer(recognizer)
637637
}
638638

639+
setupTextLayoutManager(textLayoutManager)
639640
setSelectedTextRange(NSTextRange(location: textLayoutManager.documentRange.location), updateLayout: false)
640641
}
641642

Sources/STTextViewUIKit/STTextView.swift

+5-4
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ import STTextViewCommon
3636
/// The manager that lays out text for the text view's text container.
3737
@objc dynamic open var textLayoutManager: NSTextLayoutManager {
3838
willSet {
39+
textContentManager.primaryTextLayoutManager = nil
3940
textContentManager.removeTextLayoutManager(newValue)
4041
}
4142
didSet {
4243
textContentManager.addTextLayoutManager(textLayoutManager)
4344
textContentManager.primaryTextLayoutManager = textLayoutManager
4445
setupTextLayoutManager(textLayoutManager)
46+
self.text = text
4547
}
4648
}
4749

@@ -444,13 +446,9 @@ import STTextViewCommon
444446

445447
super.init(frame: frame)
446448

447-
setupTextLayoutManager(textLayoutManager)
448-
449449
contentView.clipsToBounds = clipsToBounds
450450
lineHighlightView.clipsToBounds = clipsToBounds
451451

452-
setSelectedTextRange(NSTextRange(location: textLayoutManager.documentRange.location), updateLayout: false)
453-
454452
addSubview(contentView)
455453
contentView.addSubview(lineHighlightView)
456454

@@ -462,6 +460,9 @@ import STTextViewCommon
462460

463461
updateEditableInteraction()
464462
isGutterVisible = showsLineNumbers
463+
464+
setupTextLayoutManager(textLayoutManager)
465+
setSelectedTextRange(NSTextRange(location: textLayoutManager.documentRange.location), updateLayout: false)
465466
}
466467

467468
@available(*, unavailable)

0 commit comments

Comments
 (0)