仪器在释放后仍抱怨内存泄漏

时间:2017-06-28 22:07:58

标签: ios objective-c memory-leaks

我有以下内容用于创建和释放对象,但是仪器仍在抱怨内存泄漏。所有可能的泄漏对象都已发布。我不确定发布的顺序是否重要。

    - (void)drawRect:(CGRect)rect {
    // Drawing code
    // drawing the gradiant from left to right and from green to yellow to red
    // the position of yellow should be
    UIColor *greenColor = [CNVColorUtils colorWithHexString:@"#09c961"];
    UIColor *redColor = [CNVColorUtils colorWithHexString:@"#ff3431"];
    UIColor *neutralColor = [CNVColorUtils colorWithHexString:@"#fff00c"];

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGFloat greenEnd = self.positivePercentage ?: kCNVSentimentsBarMinPersentage;
    CGFloat yellowEnd = greenEnd + self.neutralPercentage ?: kCNVSentimentsBarMinPersentage;

    CGFloat firstGradientStart = MAX(0, greenEnd - kCNVSentimentsBarTransactionPercentage);
    CGFloat firstGradientEnd = MIN(yellowEnd, greenEnd + kCNVSentimentsBarTransactionPercentage);

    CGFloat secondGradientStart = MAX(greenEnd, yellowEnd - kCNVSentimentsBarTransactionPercentage);
    CGFloat secondGradientEnd = MIN(1.0, yellowEnd + kCNVSentimentsBarTransactionPercentage);

    NSArray *colors = @[(id)greenColor.CGColor, (id)greenColor.CGColor, (id)neutralColor.CGColor, (id)neutralColor.CGColor, (id)redColor.CGColor,(id)redColor.CGColor];

    CGFloat locations[6] = {0.0, firstGradientStart, firstGradientEnd, secondGradientStart, secondGradientEnd, 1.0};

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)colors, locations);
    CGContextDrawLinearGradient(context, gradient, CGPointMake(0.0, 0.0), CGPointMake(CGRectGetMaxX(self.frame), 0.0), 0);

    CGColorSpaceRelease(colorSpace);
    CGGradientRelease(gradient);

}

仪器抱怨泄漏对象为CGGradient

感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

通过Xcode的“产品”菜单上的静态分析器(命令 + shift + B 或“Analyze”)运行代码。找到这些问题非常好。但我在上面的代码中没有看到任何明显的泄漏。

但是,这里存在一个问题,即您colorRef过度释放,因为NSArray和ARC应该为您管理该数组。但这是一个过度释放,而不是泄漏。 [此引用的有问题的代码已被编辑出来。]