在便捷方法和NSClassFromString(...)alloc/release中找到LLVM/Clang错误

问题描述:

我正在用LLVM/Clang静态分析器分析Objective-C iPhone项目。我不断收到两个报告的错误,但我确信代码是正确的。在便捷方法和NSClassFromString(...)alloc/release中找到LLVM/Clang错误

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... 

这些叮当问题或我完全错误在这两种情况下?

在这两种情况下,您的代码看起来都是正确的。对于没有。 2,您可能会使用performSelector而不是简单的initWithAdditive(您是否有使用选择器的特殊原因?)来混淆分析仪。我不确定没有。 1,但也许尝试初始化它与[[[UILabel alloc] init...] autorelease]而不是单独自动释放,并查看问题是否仍然存在。

+0

谢谢。案例2解决了,似乎分析器真的被前面的代码弄糊涂了。 应该是: UIViewController * detailsViewController = [[detailsViewControllerClass alloc] initWithAdditive:additive]; 案例1仍然没有解决。 – pirags 2010-05-17 15:56:51

+0

案例1很神秘。错误消息通常发生在错误命名方法(例如,在不返回保留对象的情况下命名方法'newFoo')时。我没有看到任何会导致它在你的代码中。这可能是分析仪中的一个错误。 – shosti 2010-05-17 16:13:03

+0

如果您将autorelease绑定到init或返回,case 1中的问题是否会消失?无论如何,你应该向苹果公司报告这个bug - http://bugreporter.apple.com – JeremyP 2010-05-18 10:59:05