views:

24

answers:

1

How reliable is Xcode's "Build and Analyze" ?

I keep looking for the "potential leak" that it finds - but don't understand it.

Here the code:

-(void)viewDidLoad{

    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *aBI = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                        target:self action:@selector(add:)];

    self.navigationItem.rightBarButtonItem = aBI;
    aBI.release;

    UIBarButtonItem *eB = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
                                                                        target:self action:@selector(EinstellungenTapped:)];
    UIBarButtonItem * space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];


    UIBarButtonItem *emailB = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCompose
                                                                       target:self action:@selector(emailTapped:)];


    NSArray* toolbarItems = [NSArray arrayWithObjects: eB,space,emailB,
                             nil];

    [self setToolbarItems: toolbarItems];

    eB.release;      // <------- Potential leak ??????
    emailB.release;  // <------- Potential leak ??????
    space.release;  // <------- Potential leak ??????
    }

Why should these three be potential leaks?

Even without the assignemt to the NSARRAY it's telling me about leaks.

What is wrong here?

many thanks....

+1  A: 

I imagine the problem is because you're horribly mis-using dot syntax and confusing the static analyzer. You should never ever type anObject.release, because that has the completely wrong meaning. Dot syntax is designed for accessing properties, and has that specific semantic meaning, e.g. accessing a property. Anything which modifies the object itself should be a method, not a property, and should be called as such.

In other words, change those to

[eB release];
[emailB release];
[space release];

and things should work fine. Now the static analyzer should recognize that you really are releasing the objects.

As one final note, the fact that eB.release technically works can be considered an artifact of how the compiler works. While I don't believe that will ever break, I also don't know if it's guaranteed not to, and it's very bad form to rely upon.

Kevin Ballard
Yes, I can't believe it - I totally overlooked those - of course, you are absolutely right.Thanks very much for the extra pair of eyes!