ios - Instrument 释放后仍报内存泄漏

标签 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
任何帮助表示赞赏。

最佳答案

通过静态分析器运行代码(command+shift+B 或 Xcode 的“产品”菜单上的“分析”)。它非常擅长发现这类问题。但我在上面的代码中没有看到任何明显的泄漏。

但是,这里有一个问题,即您过度释放了 colorRef因为 NSArray并且 ARC 应该为您管理该阵列。但这是过度释放,而不是泄漏。 [此引用的违规代码已被编辑,无法解决。]

关于ios - Instrument 释放后仍报内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44813313/

相关文章:

ios - Xcode Interface Builder 高度/宽度设置与 View Controller

iphone - 删除使用 ShareKit 框架打开的 twitter App 中的 Cancel Link

ios - 如何检查 UITableview 中 UIButtons 的条件

ios - 计算运动时间CMMotionActivity?

ios - 捆绑标识符和产品名称中区分大小写

objective-c - CMYK NSImage 获取像素数据

iphone - 如何确定 iPhone 上的默认网关?

iphone - ABPeoplePickerNavigationController 内存泄漏?

c++ - 嵌套对象中的内存不可用

android - 将 Activity 上下文传递到静态方法中,内存泄漏的可能性?