我有以下用于创建和释放对象的方法,但仪器仍然提示内存泄漏。所有可能的泄漏对象都已释放。我不确定释放的顺序是否重要。
- (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/