在便捷方法和 NSClassFromString(...) 分配/释放中发现 LLVM/Clang 错误

发布于 2024-09-02 00:31:06 字数 1008 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽手叙旧 2024-09-09 00:31:06

您的代码在这两种情况下看起来都是正确的。对于没有。 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 plain initWithAdditive (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文