【发布时间】:2017-06-28 22:07:58
【问题描述】:
我有以下用于创建和释放对象的方法,但仪器仍然抱怨内存泄漏。所有可能的泄漏对象都已释放。我不确定释放的顺序是否重要。
- (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
感谢任何帮助。
【问题讨论】:
-
您在使用 ARC 吗?还是手动释放? (如果是后者,你能说明你在哪里做吗?)
-
我相信我正在使用 ARC
-
谢谢,我可能会看看如何使用静态分析器@Rob
-
如果你使用 arc 那么你不需要分配或删除内存,它已经为你完成了。但如果你不是,那么你需要释放每一个东西,有点像在
C编码中你必须使用malloc和free。无论如何,@Rob 是正确的,静态分析器经常使用,所以你现在应该使用他的建议:D -
顺便说一句,在未来,您可以斩钉截铁并使用
CAGradientLayer,这样您就无需手动管理所有这些CoreFoundation 内存引用。但是,如果以上内容准确地代表了正在泄漏的代码,那么它可能没有实际意义,因为我怀疑泄漏仍然存在于其他地方。
标签: ios objective-c memory-leaks