Xcode 4静态代码分析问题

发布于 2024-11-19 09:01:08 字数 870 浏览 3 评论 0原文

这是我之前关于 Xcode 4 静态分析器的问题的后续内容。这并不是一个特别的问题,因为我现在的代码可以按需要工作,但我只是想知道幕后是如何工作的。考虑以下代码:

- (IBAction)cameraButtonPressed:(id)sender
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO)
    {
        return;
    }

    UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
    cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
    cameraUI.allowsEditing = NO;
    cameraUI.delegate = self;
    [self presentModalViewController:cameraUI animated:YES];

    NSString *theString = [[NSString alloc] initWithString:@"cameraButtonPressed done"];
    NSLog(@"%@", theString);
}

对我来说,这段代码看起来有两个对象(cameraUI 和 theString)需要释放。然而,analyze 函数正确地识别出只有 theString 需要在方法结束时释放,即使这两个对象都是从 alloc-init 返回的,根据我的经验,这始终意味着您在完成后释放。

我这里的问题是,静态代码分析器如何知道不将cameraUI标记为问题?

This is the follow up to my question earlier about the Xcode 4 static analyzer. It is not specifically a problem since I have the code now working as it needs to, but I am just wondering how things are working behind the scenes. Consider the following code:

- (IBAction)cameraButtonPressed:(id)sender
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO)
    {
        return;
    }

    UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
    cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
    cameraUI.allowsEditing = NO;
    cameraUI.delegate = self;
    [self presentModalViewController:cameraUI animated:YES];

    NSString *theString = [[NSString alloc] initWithString:@"cameraButtonPressed done"];
    NSLog(@"%@", theString);
}

To me, the way this code looks, there are two objects (cameraUI and theString) that need to be released. However, the analyze function correctly identifies that only theString needs to be released at the end of the method, even though both objects are returned from alloc-init, which in my experience has always meant that you release when you are done.

The question I have here is, how does the static code analyzer know not to flag cameraUI as an issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

迷途知返 2024-11-26 09:01:08

我将其称为静态分析器的错误。分配给cameraUI的UIImagePickerController实例应该在非垃圾收集环境(如iOS)中释放或自动释放。

I would call this a bug with the static analyzer. The UIImagePickerController instance assigned to cameraUI should be released or autoreleased in a non-garbage-collected environment (like iOS).

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