Xcode 静态分析器和 copyWithZone
Xcode 4 静态分析器将此方法标记为具有过度释放的返回值,而事实似乎并非如此。
- (id)copyWithZone:(NSZone *)zone
{
return [[[self class] allocWithZone:zone] initWithURL:self.url postString:self.postString];
}
有一个箭头从 return 关键字指向其后面的表达式,另一个箭头从该表达式指向分析器警告。下面是静态分析:
- 方法返回一个带有 +1 保留计数的 Objective-C 对象
- 发送了自动释放消息
- 对象作为拥有引用返回给调用者(单个保留计数传输给调用者)
- 对象返回给调用者带有 +0(非-owning) 保留计数
- 具有 +0 保留计数的对象返回给调用者,其中预期 +1(拥有)保留计数
静态分析器是否不正确或者此代码是否有问题?
根据请求,-initWithURL:postString:
方法:
- (id)initWithURL:(NSURL *)u postString:(NSString *)p
{
if ( (self = [super init]) )
{
self.url = u;
self.postString = p;
}
return self;
}
即使在清理和重建项目后,我仍然收到此警告。
更新:升级到 Xcode 4.2 后,Xcode 静态分析器不再将此标记为问题。
The Xcode 4 static analyzer flags this method as a having an over-released return value when that does not seem to be the case.
- (id)copyWithZone:(NSZone *)zone
{
return [[[self class] allocWithZone:zone] initWithURL:self.url postString:self.postString];
}
There is an arrow pointing from the return keyword to the expression following it, and another from that expression to the analyzer warning. Here is the static analysis:
- Method returns an Objective-C object with a +1 retain count
- Object sent -autorelease message
- Object returned to caller as an owning reference (single retain count transferred to caller)
- Object returned to caller with a +0 (non-owning) retain count
- Object with +0 retain counts returned to caller where a +1 (owning) retain count is expected
Is the static analyzer incorrect or is there something wrong with this code?
By request, the -initWithURL:postString:
method:
- (id)initWithURL:(NSURL *)u postString:(NSString *)p
{
if ( (self = [super init]) )
{
self.url = u;
self.postString = p;
}
return self;
}
I continue to get this warning even after cleaning and rebuilding the project.
UPDATE: The Xcode static analyzer no longer flags this as an issue after upgrading to Xcode 4.2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 Xcode 中的一个错误。代码没问题。
That's a bug in Xcode. The code is alright.