Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Updated: use __unsafe_unretained and fix key from CTParagraphStyleRef #166

Merged
merged 2 commits into from

2 participants

@tonycn

The las commit should be ok, through there is a trick for the key, but cleaner than the previous change.

Finally two changes:
1) Use __unsafe_unretained to be compatible with ios4 and ios5;
2) Fix key of CTParagraphStyleRef by remove memory address info from "<" char to the end of string

I was running DemoApp for allocation profiling by instrument.
Made a heap, and token snapshots for repeat step "Enter ReadMe Sample -> Back to Home -> Enter ReadMe Sample -> Back to Home ..."
I found there were 10+ DTCoreTextParagraphStyle objects increased each time.
And instrument tell that most come from "NSString *key = [(__bridge id)ctParagraphStyle description];"

Solved the issue in a inelegant way by format all the values needed.

@Cocoanetics
Owner

How is that a memory leak? Details, please.

@tonycn

@Cocoanetics I was running DemoApp for allocation profiling by instrument. Made a heap, and token snapshots for repeat step "Enter ReadMe Sample -> Back to Home -> Enter ReadMe Sample -> Back to Home ..." I found there were 10+ DTCoreTextParagraphStyle objects increased each time. And instrument tell that most come from "NSString *key = [(__bridge id)ctParagraphStyle description];"

@tonycn

Cannot figure out the reason yet.

@tonycn

Finally found the reason, the description for key is like following. "<CFArray 0x7ed43d0 [0x20eab48]>" diffs each time, so the DTCoreTextParagraphStyle cache is never used.

Description of CTParagraphStyleRef
CTParagraphStyle:
writing direction = -1, alignment = 4, line break mode = 0, default tab interval = 36
first line head indent = 0, head indent = 27, tail indent = 0
line height multiple = 0, maximum line height = 0, minimum line height = 0
line spacing adjustment = 0, paragraph spacing = 0, paragraph spacing before = 0
tabs:
<CFArray 0x7ed43d0 [0x20eab48]>{type = immutable, count = 2, values = (
0 : CTTextTab: location = 22, alignment = 1, options = (none)

1 : CTTextTab: location = 27, alignment = 0, options = (none)

)}

@tonycn tonycn Rollback 2 commits; Change block weak ref variable compile flag to __…
…unsafe_unretained to be compatible with ios4 and ios5; fix key of CTParagraphStyleRef by remove address info
07a4dd5
@Cocoanetics

I suspect that this might cause problems when comparing a paragraph style that is identical to another one except for the tab stops. Can you instead have a function that creates a nice well-formed key for a given CTParagraphStyleRef?

@tonycn

@Cocoanetics just removed the address from description string of CTParagraphStyleRef

@Cocoanetics Cocoanetics merged commit 0c38cf0 into from
@Cocoanetics
Owner

Thanks for the work on the fixes!

@tonycn

It's a pleasure. I am going to use this lib in the app. Thanks for the cool framework for html render on iOS!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 10, 2012
  1. @tonycn

    Rollback 2 commits; Change block weak ref variable compile flag to __…

    tonycn authored
    …unsafe_unretained to be compatible with ios4 and ios5; fix key of CTParagraphStyleRef by remove address info
  2. @tonycn
This page is out of date. Refresh to see the latest.
View
2  Core/Source/DTAttributedTextContentView.m
@@ -660,7 +660,7 @@ - (DTCoreTextLayoutFrame *)layoutFrame
if (_delegateFlags.delegateSupportsNotificationBeforeTextBoxDrawing)
{
- __block __weak DTAttributedTextContentView *weakself = self;
+ __unsafe_unretained DTAttributedTextContentView *weakself = self;
[_layoutFrame setTextBlockHandler:^(DTTextBlock *textBlock, CGRect frame, CGContextRef context, BOOL *shouldDrawDefaultBackground) {
BOOL result = [weakself->_delegate attributedTextContentView:weakself shouldDrawBackgroundForTextBlock:textBlock frame:frame context:context forLayoutFrame:weakself->_layoutFrame];
View
21 Core/Source/DTCoreTextParagraphStyle.m
@@ -42,6 +42,23 @@ + (DTCoreTextParagraphStyle *)defaultParagraphStyle
return [[DTCoreTextParagraphStyle alloc] init];
}
++ (NSString *)niceKeyFromParagraghStyle:(CTParagraphStyleRef)ctParagraphStyle {
+
+ // this is naughty: CTParagraphStyle has a description
+ NSString *key = [(__bridge id)ctParagraphStyle description];
+
+ NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"0x[0123456789abcdef]{1,8}"
+ options:NSRegularExpressionCaseInsensitive
+ error:nil];
+
+ NSString *newKey = [regex stringByReplacingMatchesInString:key
+ options:0
+ range:NSMakeRange(0, [key length])
+ withTemplate:@""];
+
+ return newKey;
+}
+
+ (DTCoreTextParagraphStyle *)paragraphStyleWithCTParagraphStyle:(CTParagraphStyleRef)ctParagraphStyle
{
DTCoreTextParagraphStyle *returnParagraphStyle = NULL;
@@ -58,9 +75,7 @@ + (DTCoreTextParagraphStyle *)paragraphStyleWithCTParagraphStyle:(CTParagraphSty
dispatch_semaphore_wait(selfLock, DISPATCH_TIME_FOREVER);
{
- // this is naughty: CTParagraphStyle has a description
- NSString *key = [(__bridge id)ctParagraphStyle description];
-
+ NSString *key = [self niceKeyFromParagraghStyle:ctParagraphStyle];
returnParagraphStyle = [_paragraphStyleCache objectForKey:key];
if (!returnParagraphStyle)
Something went wrong with that request. Please try again.