在便捷方法和 NSClassFromString(...) 分配/释放中发现 LLVM/Clang 错误
我正在使用 LLVM/Clang 静态分析器分析 Objective-C iPhone 项目。我不断收到两个报告的错误,但我很确定代码是正确的。
1)方便的方法。
+ (UILabel *)simpleLabel
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 10, 200, 25)];
label.adjustsFontSizeToFitWidth = YES;
[label autorelease]; // Object with +0 retain counts returned to caller where a +1 (owning) retain count is expected.
return label;
}
2) [NSClassFromString(...) alloc] 返回retainCount + 1。我说得对吗?
Class detailsViewControllerClass =
NSClassFromString(self.detailsViewControllerName);
UIViewController *detailsViewController =
[[detailsViewControllerClass alloc]
performSelector:@selector(initWithAdditive:) withObject:additive];
[self.parentController.navigationController
pushViewController:detailsViewController animated:YES];
[detailsViewController release]; // Incorrect decrement of the reference count of an object is not owned...
这些是一些 Clang 问题还是我在这两种情况下都完全错误了?
I am analyzing Objective-C iPhone project with LLVM/Clang static analyzer. I keep getting two reported bugs, but I am pretty sure that the code is correct.
1) Convenience method.
+ (UILabel *)simpleLabel
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 10, 200, 25)];
label.adjustsFontSizeToFitWidth = YES;
[label autorelease]; // Object with +0 retain counts returned to caller where a +1 (owning) retain count is expected.
return label;
}
2) The [NSClassFromString(...) alloc] returns retainCount + 1. Am I right?
Class detailsViewControllerClass =
NSClassFromString(self.detailsViewControllerName);
UIViewController *detailsViewController =
[[detailsViewControllerClass alloc]
performSelector:@selector(initWithAdditive:) withObject:additive];
[self.parentController.navigationController
pushViewController:detailsViewController animated:YES];
[detailsViewController release]; // Incorrect decrement of the reference count of an object is not owned...
Are these some Clang issues or I am totally mistaken in these both cases?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码在这两种情况下看起来都是正确的。对于没有。 2、您可能通过使用
performSelector
而不是普通的initWithAdditive
来混淆分析器(您使用选择器有什么特殊原因吗?)。我不确定是否可以。 1,但也许尝试使用[[[UILabel alloc] init...] autorelease]
初始化它,而不是单独使用 autorelease,然后看看问题是否仍然存在。Your code looks correct in both cases. For no. 2, you're probably confusing the analyzer by using
performSelector
instead of plaininitWithAdditive
(is there a particular reason you're using a selector?). I'm not sure about no. 1, but maybe try initializing it with[[[UILabel alloc] init...] autorelease]
instead of autoreleasing separately, and see if the problem persists.