潜在泄漏(使用垃圾收集时)
我正在我的 Cocoa 应用程序上运行具有自动垃圾收集功能的分析,并收到以下错误消息:
Potential leak (when using garbage collection) of an object allocated on line 1243
这是第 1243 行的内容:
self.positiveValueColor = CGColorCreateGenericRGB(0.0, 0.0, 1.0, 1.0);
这是 self.positiveValueColor 属性的定义:
@property (assign) CGColorRef positiveValueColor
不过,分析器稍后会报告错误,放在它下面的方法中。 “double MaximumValue = 0.0”是错误出现的地方,即使它引用了第 1243 行:
以下是整个方法供参考:
- (void) setDefaultColors {
if (self.positiveValueColor == nil) {
self.positiveValueColor = CGColorCreateGenericRGB(0.0, 0.0, 1.0, 1.0);
}
if (self.negativeValueColor == nil) {
self.negativeValueColor = CGColorCreateGenericRGB(1.0, 0.0, 0.0, 1.0);
}
if (self.zeroValueColor == nil) {
self.zeroValueColor = CGColorGetConstantColor(kCGColorBlack);
}
}
- (BOOL) largestValueIsPositive {
double largestValue = 0.0;
if (self.pv != nil) {
double value = [self.pv doubleValue];
if (fabs(value) > fabs(largestValue)) {
largestValue = value;
}
}
... // method continues on
为什么我会收到该分析错误?
- 编辑 -
谢谢,查克!那行得通。这是我用以下内容替换了相关行:
self.positiveValueColor = (CGColorRef)CFMakeCollectable(CGColorCreateGenericRGB(0.0, 0.0, 1.0, 1.0));
I'm running an Analyze on my Cocoa app with automatic garbage collection and receiving the following error message:
Potential leak (when using garbage collection) of an object allocated on line 1243
This is what is on line 1243:
self.positiveValueColor = CGColorCreateGenericRGB(0.0, 0.0, 1.0, 1.0);
Here is the definition of the self.positiveValueColor property:
@property (assign) CGColorRef positiveValueColor
The Analyzer is reporting the error later on though, down in the method below it. "double largestValue = 0.0" is where the error appears, even though it references line 1243:
Here is the entire method for reference:
- (void) setDefaultColors {
if (self.positiveValueColor == nil) {
self.positiveValueColor = CGColorCreateGenericRGB(0.0, 0.0, 1.0, 1.0);
}
if (self.negativeValueColor == nil) {
self.negativeValueColor = CGColorCreateGenericRGB(1.0, 0.0, 0.0, 1.0);
}
if (self.zeroValueColor == nil) {
self.zeroValueColor = CGColorGetConstantColor(kCGColorBlack);
}
}
- (BOOL) largestValueIsPositive {
double largestValue = 0.0;
if (self.pv != nil) {
double value = [self.pv doubleValue];
if (fabs(value) > fabs(largestValue)) {
largestValue = value;
}
}
... // method continues on
Why am I getting that analyze error?
-- EDIT --
Thanks, Chuck! That worked. Here is what I replaced the relevant lines with:
self.positiveValueColor = (CGColorRef)CFMakeCollectable(CGColorCreateGenericRGB(0.0, 0.0, 1.0, 1.0));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CGColorRefs 通常不适合垃圾回收。您应该使用 CFMakeCollectable()。这就是它的警告。
CGColorRefs are not ordinarily eligible for garbage collection. You should use CFMakeCollectable(). That's what it's warning about.