核心图形可可内存泄漏

Mat*_*oal 0 macos cocoa memory-leaks objective-c ios

我无法理解这段代码中泄漏了什么.我得到这两个泄漏:

在此输入图像描述

我不知道如何提供更多信息,而不是直接在这里添加代码:

    float value = [[dict objectForKey:@"value"] floatValue];
    UIColor *color = (UIColor*)[dict objectForKey:@"color"];
    NSString *type = [dict objectForKey:@"type"];

    CAShapeLayer *circle = [CAShapeLayer layer];
    circle.path = CGPathCreateWithEllipseInRect(CGRectMake(0, 0, LABELSIZE, LABELSIZE), nil);
    circle.fillColor = color.CGColor;
    circle.contentsScale = 2.0;

    CATextLayer *text = [CATextLayer layer];
    text.contentsScale = 2.0;
    text.string = [NSString stringWithFormat:@"%d%%",(int)value];
    text.frame = CGRectMake(0, LABELSIZE/2 - LABELFONTSIZE/2 -1.0, LABELSIZE, LABELFONTSIZE);
    text.fontSize = 10;
    text.alignmentMode = kCAAlignmentCenter;
    text.foregroundColor = [UIColor colorWithWhite:0.0 alpha:0.6].CGColor;
    text.font = (__bridge CFTypeRef)([UIFont fontWithName:@"Avenir-Medium" size:LABELFONTSIZE]);

    [circle addSublayer:text];

    if([type isEqualToString:@"IN"]){
        circle.position = CGPointMake(0, self.layer.frame.size.height / 2 - LABELSIZE/2);
    }else{
        circle.position = CGPointMake(self.layer.frame.size.width - LABELSIZE, self.layer.frame.size.height / 2 - LABELSIZE/2);
    }

    [self.labelsLayer addSublayer:circle];
Run Code Online (Sandbox Code Playgroud)

Ano*_*dya 7

您的泄漏错误是由于

    circle.path = CGPathCreateWithEllipseInRect(CGRectMake(0, 0, LABELSIZE, LABELSIZE), nil);
Run Code Online (Sandbox Code Playgroud)

如您所知,CGXxx和CFXxx不处理ARC,您需要手动释放内存.因此,释放您在上面创建的内存.

编辑:(由Rob评论)

你必须打电话CGPathRelease().对于您在名称中使用"创建"或"复制"调用的每个Core Graphics方法,您必须CGXXXRelease手动调用相应的Core Graphics 方法(或者,在某些情况下,您可以将所有权转移到ARC).