views:

339

answers:

5

The Leaks instrument tells me that I have a leak in this code fragment. Why is this so?

This code fragment is in viewDidLoad().

UINavigationItem *navItem=[self navigationItem];

UIBarButtonItem *addFeed = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addFeed)];
[navItem setRightBarButtonItem:addFeed]; // leaks says that 128 bytes leaked
[addFeed release];

UIBarButtonItem *reload = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh  target:self action:@selector(reload)];
[navItem setLeftBarButtonItem:reload]; // leaks says that 128 bytes leaked here too !
[reload release];
[navItem release];
+4  A: 

You should not be releasing navItem. You did not alloc/retain/new/create it, so you do not release it.

Other than that, your code looks fine. Is that everything in the method?

Dave DeLong
+2  A: 

The leaks instrument only tells you where the leaked memory was allocated; it can't tell you where the memory should have been released but wasn't since there's no possible way for it to know that. Your leak is occurring elsewhere.

This code is mostly fine, except you should not be releasing navItem at the end. You are not an owner of it, since you didn't create it with a method named alloc, new, or copy in its name, so you should not release it.

Adam Rosenfield
A: 

Do you have NSZombieEnabled? This causes objects not to be retained by the NSZombie instances and you'll see "leaks" when running the Leaks tool.

nall
A: 

It seems that you're not releasing the view controller with custom viewDidLoad method.

Valerii Hiora
+1  A: 

if you're still getting the leak message and can't track down the bug, you can try using the static analyzer included in the latest and greatest Xcode (version 3.2)

Build > Build and Analyze

it'll use LLVM-Clang to statically analyze your code in a pretty way.

http://developer.apple.com/mac/library/featuredarticles/StaticAnalysis/index.html

UPDATE:

in your code snippet:

UINavigationItem *navItem=[self navigationItem];

UIBarButtonItem *addFeed = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addFeed)];
[navItem setRightBarButtonItem:addFeed]; // leaks says that 128 bytes leaked
[addFeed release];

your leak is probably coming from setting the new rightBarButtonItem without releasing the old one.

this is what i think is happening:

1) get a handle to the navigationItem (has right bar button A)

2) create a new UIBarButton Item (making right bar button B)

3) setRightBarButtonItem to button B

now where's Button A? it should have been released by navItem when you set the new button. so you could have forgotten to release the button when you set it the first time, or you've got it retained somewhere else.

pxl
some more thoughts... rightBarButtonItem is @property(nonatomic, retain) UIBarButtonItem *rightBarButtonItem, so it should release the old one before retaining the new one.so maybe somewhere else in your code you're retaining the old button or not releasing it after setting it the first time
pxl