Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit 076d22e

Browse files
garrettmoonAdlai Holler
authored andcommitted
Do not reuse NSStringDrawingContexts (#2537)
1 parent ff9e493 commit 076d22e

1 file changed

Lines changed: 12 additions & 18 deletions

File tree

AsyncDisplayKit/TextKit/ASTextKitRenderer.mm

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,11 @@
3535
return truncationCharacterSet;
3636
}
3737

38-
@interface ASTextKitRenderer()
39-
/**
40-
* This object is lazily created. It is provided to the NSAttributedString
41-
* drawing methods used by the fast-paths in the size calculation and drawing
42-
* instance methods.
43-
*/
44-
@property (nonatomic, strong, readonly) NSStringDrawingContext *stringDrawingContext;
45-
@end
46-
4738
@implementation ASTextKitRenderer {
4839
CGSize _calculatedSize;
4940
BOOL _sizeIsCalculated;
5041
}
51-
@synthesize attributes = _attributes, context = _context, shadower = _shadower, truncater = _truncater, fontSizeAdjuster = _fontSizeAdjuster, stringDrawingContext = _stringDrawingContext;
42+
@synthesize attributes = _attributes, context = _context, shadower = _shadower, truncater = _truncater, fontSizeAdjuster = _fontSizeAdjuster;
5243

5344
#pragma mark - Initialization
5445

@@ -118,15 +109,18 @@ - (ASTextKitContext *)context
118109

119110
- (NSStringDrawingContext *)stringDrawingContext
120111
{
121-
if (_stringDrawingContext == nil) {
122-
_stringDrawingContext = [[NSStringDrawingContext alloc] init];
123-
124-
if (isinf(_constrainedSize.width) == NO && _attributes.maximumNumberOfLines > 0) {
125-
ASDisplayNodeAssert(_attributes.maximumNumberOfLines != 1, @"Max line count 1 is not supported in fast-path.");
126-
[_stringDrawingContext setValue:@(_attributes.maximumNumberOfLines) forKey:@"maximumNumberOfLines"];
127-
}
112+
// String drawing contexts are not safe to use from more than one thread.
113+
// i.e. if they are created on one thread, it is unsafe to use them on another.
114+
// Therefore we always need to create a new one.
115+
//
116+
// http://web.archive.org/web/20140703122636/https://developer.apple.com/library/ios/documentation/uikit/reference/NSAttributedString_UIKit_Additions/Reference/Reference.html
117+
NSStringDrawingContext *stringDrawingContext = [[NSStringDrawingContext alloc] init];
118+
119+
if (isinf(_constrainedSize.width) == NO && _attributes.maximumNumberOfLines > 0) {
120+
ASDisplayNodeAssert(_attributes.maximumNumberOfLines != 1, @"Max line count 1 is not supported in fast-path.");
121+
[stringDrawingContext setValue:@(_attributes.maximumNumberOfLines) forKey:@"maximumNumberOfLines"];
128122
}
129-
return _stringDrawingContext;
123+
return stringDrawingContext;
130124
}
131125

132126
#pragma mark - Sizing

0 commit comments

Comments
 (0)