Reputation:
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];
Upvotes: 2
Views: 985
Reputation: 19664
[navItem setRightBarButtonItem:addFeed];
[navItem setLeftBarButtonItem:reload];
You are creating copies of the objects in these accessors. These accessors are incrementing the retainCount by 1. Your accessors should release each object and then immediately retain them.
Example:
- (void) setTitle: (NSString*) newTitle {
if (title != newTitle) {
[title release];
title = [newTitle retain]; // Or copy, depending on your needs.
}
Take a look at the techniques here: Memory Management Programming
I believe that's what's up. So take a hard look at those two accessors.
Upvotes: 0
Reputation: 1297
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.
Upvotes: 2
Reputation: 1552
It seems that you're not releasing the view controller with custom viewDidLoad
method.
Upvotes: 0
Reputation: 16139
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.
Upvotes: 0
Reputation: 400274
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.
Upvotes: 3
Reputation: 243156
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?
Upvotes: 6