ios - 转换为 ARC 的代码中的潜在泄漏

标签 ios objective-c automatic-ref-counting

我已经将一个库的一些代码翻译成 ARC,我怀疑我没有正确地做到这一点。基本上我已经添加了 som __bridge代码的命令。 Xcode 在分析期间提示,说存储在 path 中的对象可能存在泄漏。 .代码附在下面。您能帮我解决倒数第二行的潜在泄漏问题吗:

-(void)drawRect:(CGRect)rect {
    if(self.text.length<=0) {
        self.text = EMPTY;
        return;
    }

    //Prepare View for drawing
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetTextMatrix(context,CGAffineTransformIdentity);
    CGContextTranslateCTM(context,0,([self bounds]).size.height);
    CGContextScaleCTM(context,1.0,-1.0);

    //Get the view frame size
    CGSize size = self.frame.size;

    //Determine default text color
    UIColor* textColor = nil;
    if(!self.highlightColor||!(textColor=[self.highlightColor objectForKey:kRegexHighlightViewTypeText])) {
        if([self.textColor isEqual:[UIColor clearColor]]) {
            if(!(textColor=[[RegexHighlightView highlightTheme:kRegexHighlightViewThemeDefault] objectForKey:kRegexHighlightViewTypeText]))
               textColor = [UIColor blackColor];
        } else textColor = self.textColor;
    }

    //Set line height, font, color and break mode
    CGFloat minimumLineHeight = [self.text sizeWithFont:self.font].height,maximumLineHeight = minimumLineHeight;
    CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)self.font.fontName,self.font.pointSize,NULL);
    CTLineBreakMode lineBreakMode = kCTLineBreakByWordWrapping;

    //Apply paragraph settings
    CTParagraphStyleRef style = CTParagraphStyleCreate((CTParagraphStyleSetting[3]){
        {kCTParagraphStyleSpecifierMinimumLineHeight,sizeof(minimumLineHeight),&minimumLineHeight},
        {kCTParagraphStyleSpecifierMaximumLineHeight,sizeof(maximumLineHeight),&maximumLineHeight},
        {kCTParagraphStyleSpecifierLineBreakMode,sizeof(CTLineBreakMode),&lineBreakMode}
    },3);
    NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)font,(NSString*)kCTFontAttributeName,(__bridge id)textColor.CGColor,(NSString*)kCTForegroundColorAttributeName,(__bridge id)style,(NSString*)kCTParagraphStyleAttributeName,nil];

    //Create path to work with a frame with applied margins
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path,NULL,CGRectMake(MARGIN+0.0,(-self.contentOffset.y+0),(size.width-2*MARGIN),(size.height+self.contentOffset.y-MARGIN)));


    //Create attributed string, with applied syntax highlighting
    CFAttributedStringRef attributedString = (__bridge CFAttributedStringRef)[self highlightText:[[NSAttributedString alloc] initWithString:self.text attributes:attributes]];

    //Draw the frame
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString);
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0,CFAttributedStringGetLength(attributedString)),path,NULL);
    CTFrameDraw(frame,context);
}

Here is a preview from the profiling after releasing path

最佳答案

HaIR 的定义有点不完整:编译器只管理 Objective-C 类型。 create 之后的任何内容规则,或者是 malloc 'd, calloc 'd 或 new倒堆是你的责任。您得到的内存泄漏源于 4 个不同的实例,即未将函数与 create 平衡。在它的名字中加上正确的前缀...release()称呼。

您可以通过调用 CGPathRelease() 来修复数字 1。 ,第 2 号,调用 -autorelease对于该属性字符串(假设 ARC 已关闭,在这种情况下,如果仪器在提示),数字 3 和 4 可以通过两次调用 CFRelease() 来修复。 .

关于ios - 转换为 ARC 的代码中的潜在泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16997101/

相关文章:

ios - UIImageView 不响应居中

ios - 从另一个类调用getter方法

ios - 当用户在应用程序启动区域时,Swift 4 locationManager didEnterRegion 不会触发

objective-c - 制作不受管理的 NSManagedObject 的副本

iphone - "Single NSMutableArray"与 "Multiple C-arrays"-- 哪个更高效/实用?

ios - 对象 : ARC releasing dynamically created view controller too early

iphone - 为什么我在 ARC 下比较桥接 CGColorRefs 时遇到崩溃?

ios - 隐藏标签栏时白色状态栏

ios - 在 SpriteKit 中转换到另一个 SKScene 后释放 SKScene

ios - ARC下iOS内存管理方法