Reputation: 2689
I am declaring a property for the rightbutton toolbar and allocating it like this
if(self.toolBar == nil)
self.toolBar = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = self.toolBar;
- (void)viewDidUnload {
toolBar = nil;
}
- (void)dealloc {
[toolBar release];
[super dealloc];
}
When I come to this screen for the second time(2nd time viewDidLoad called), the UIBarbuttonItem is leaking according to Instruments. What could be wrong?
Thanks for all your help in advance.
Upvotes: 1
Views: 532
Reputation: 8990
You're niling the toolbar property prematurely - viewDidUnload will be called before dealloc, thus dealloc will have no chance to release the barbutton, because toolBar
points to nil, not the object (which will remain owned (release count of at least 1) but without reference).
Plus, since you're not using the dot notation (self.toolBar
) to nil out the property, the old objects release count would not be decreased! So its retain count is at least 2 at the time your controller will quit.
I'd release the object right after assigning it to your property, because the setter method has retained it anyway (if you choose to retain it in the declaration). Later in viewDidUnload all you need to do is self.toolBar = nil;
to really get rid of it.
Upvotes: 2
Reputation: 38475
I guess your property is something like this?
@property (nonatomic, retain) IBOutlet UIBarButtonItem toolBar;
This will perform a retain automatically for you but you are giving your property an already retained toobar item.
Try this instead :
if(toolBar == nil)
toolBar = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = self.toolBar;
If you don't use self.
it won't use the property and therefor won't have that extra retain :)
Upvotes: 1