我应该如何避免这个内存警告?
我有一个返回 CGPath 并生成分析器警告的方法。该方法在协议中声明。以下是生成警告的示例实现:
“第 47 行分配并存储到‘路径’中的对象存在潜在泄漏”:
- (CGPathRef)createPathForBounds:(CGRect)bounds key:(NSString *)key;
{
if ([key isEqualToString:OvalColumn])
{
CGPathRef path = CGPathCreateWithEllipseInRect(bounds, NULL);
return path;
}
return NULL;
}
者此时不拥有的对象的引用计数的不正确递减”
CGPathRef path = [self.delegate createPathForBounds:bounds key:someKey];
// Use the path to do some drawing
CGRelease(path);
以下是生成警告的示例用法,“调用 内存管理正确;我从协议方法传回保留的 CGPath,并在调用块中释放它,因此我知道可以忽略警告,但我想将它们完全删除。
我是否缺少一个能让分析器满意的命名约定? 可以在协议中定义函数吗? 子类化将如何工作?
I have a method that returns a CGPath
and is generating analyzer warnings. The method is declared in a protocol. Here is an example implementation that is generating the warning:
"Potential leak of an object allocated on line 47 and stored into 'path'":
- (CGPathRef)createPathForBounds:(CGRect)bounds key:(NSString *)key;
{
if ([key isEqualToString:OvalColumn])
{
CGPathRef path = CGPathCreateWithEllipseInRect(bounds, NULL);
return path;
}
return NULL;
}
Here is example usage that is generating the warning, "Incorrect decrement of the reference count of an object that is not owned at this point by the caller"
CGPathRef path = [self.delegate createPathForBounds:bounds key:someKey];
// Use the path to do some drawing
CGRelease(path);
My memory management is correct; I'm passing back a retained CGPath
from my protocol method and I'm releasing it in the calling block, so I know the warnings can be ignored, but I'd like to remove them altogether.
Am I missing a naming convention that will make the analyzer happy?
Can functions be defined in protocols?
How will subclassing work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
- (CGPathRef)newPathForBounds:(CGRect)bounds key:(NSString *)key
有关该主题的详细注释可以在 此处
或者,您可以选择使用属性
cf_returns_retained
,但最好(imo)支持命名惯例。- (CGPathRef)newPathForBounds:(CGRect)bounds key:(NSString *)key
a detailed note on the topic can be found here
alternatively, you could have chosen to use the attribute
cf_returns_retained
, but it's best (imo) to favor naming conventions.