开发者

Xcode 4 static code analysis question

开发者 https://www.devze.com 2023-03-19 08:51 出处:网络
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

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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号