سؤال

xCode5 is citing a new CoreText leak that I didn't see in the previous versions of xCode. I have the following code:

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFMutableAttributedStringRef)self.text);
CGMutablePathRef mutablePath = CGPathCreateMutable();

CGPathAddRect(mutablePath, NULL, self.bounds);

self.textFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), mutablePath, NULL);

CGPathRelease(mutablePath);
CFRelease(framesetter);

And the analyzer is pointing at the last line CFRelease(framesetter) and saying "Potential leak of an object".

Does anyone how to fix this and why I'm seeing this only in xCode5?

هل كانت مفيدة؟

المحلول

you create a framesetter and never free it

self.textFrame = CTFramesetterCreateFrame(.....

== new object that is CREATED but not freed.

change to:

CTFramesetterRef newFrameSetter = CTFramesetterCreateFrame(.....
self.textFrame = newFramesetter;
if(newFramesetter) CFRelease(newFrameSetter);

as for why only xcode 5 : because it offers more warnings than before. It has been improved in that regard I'd guess

نصائح أخرى

@Dauh-Djan

but will it cause exc_bad_address problem on self.textFrame?

self.mAttributeString = [self fixLastLineMissBugWhenDrawWithCoreText:self.mAttributeString];


CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self.mAttributeString);

CGSize size = CTFramesetterSuggestFrameSizeWithConstraints(frameSetter, CFRangeMake(0, self.mAttributeString.length), NULL, CGSizeMake(self.widthLimit, CGFLOAT_MAX), NULL);
self.height = size.height + 0.5;

CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(0, 0, self.widthLimit, self.height));
NSUInteger length = self.mAttributeString.length;

CTFrameRef ctFrame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, length), path, NULL);
self.ctFrame = ctFrame;

CFRelease(ctFrame);
CGPathRelease(path);
CFRelease(frameSetter);

it solve the Potential leak of an object warning, however it will cause self.ctFrame to an exc_bad_address problem...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top