iphone - 在便捷方法和 NSClassFromString(...) alloc/release 中发现的 LLVM/Clang 错误

标签 iphone objective-c clang-static-analyzer

我正在使用 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 问题还是我在这两种情况下都完全错了?

最佳答案

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

关于iphone - 在便捷方法和 NSClassFromString(...) alloc/release 中发现的 LLVM/Clang 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2849137/

相关文章:

iphone - iPhone 3G 上的坐标全错了?这可能是你的编译器

ios - 我应该将单例存储为 ivar 吗?

iphone - 我如何知道 MKMapView 上的 setregion 何时不会触发 RegionWillChange/regionDidChange

xcode - 防止 Xcode/clang 在故意有缺陷的代码上引发逻辑错误

c++builder - 使用 clang 分析器分析 Embarcadero RAD Studio 项目

iphone - 更改正在进行的动画

ios - 使用自动布局并排放置两个 UITextField

ios - textViewDidChangeSelection 内奇怪的 UItextView 行为设置范围

objective-c - 使用 .mm 文件而不是 .m 以防万一我以后使用 C++ 是个坏主意吗?

swift - Clang Static Analyzer 可以和 Swift 一起使用吗?