views:

95

answers:

1

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?

+1  A: 

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.

eman
Thank you. Case No. 2 solved, it seems that the analyzer really was confused by the previous code.Should be:UIViewController *detailsViewController = [[detailsViewControllerClass alloc] initWithAdditive:additive];Case No. 1 still remains unresolved.
pirags
Case No. 1 is pretty mysterious. That error message usually occurs when you misname methods (for example naming a method `newFoo` without returning a retained object). I don't see anything that would cause it in your code. It might be a bug in the analyzer.
eman
Does the problem in case 1 go away if you tack autorelease on to the init or the return? Anyway, you should report the bug to Apple - http://bugreporter.apple.com
JeremyP