2010-05-17 45 views
3

我正在用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

在這兩種情況下,您的代碼看起來都是正確的。對於沒有。 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