While analysing our project Xcode came with a posibe leak notification at a custom UIBarButtonItem.
I fixed the leak, but while loading the view for the second time, [super dealloc] gives a EXC_BAD_ACCESS error.
Removing the autorelease from the UIBarButtonItem (so returning the warning):
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:toolbar] autorelease];
gives no problems while reloading the screen.
Custom UIBarButtonItem and dealloc code:
- (void)viewDidLoad
{
[super viewDidLoad];
// create a toolbar to have the buttons at the right side of the navigationBar
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 150, 44.01)];
toolbar.tintColor = [UIColor clearColor];
[toolbar setTranslucent:YES];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:4];
// Create a comments button
propertiesButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(properties)];
[buttons addObject:propertiesButton];
[propertiesButton release];
// Create a comments button
commentaryButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(comments)];
[buttons addObject:commentaryButton];
[commentaryButton release];
// create a versions button
versionsButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(versions)];
[buttons addObject:versionsButton];
[versionsButton release];
// create a save button
downloadButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:nil action:@selector(download)];
[buttons addObject:downloadButton];
[downloadButton release];
// stick the buttons in the toolbar
[toolbar setItems:buttons animated:NO];
[buttons release];
// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:toolbar] autorelease];
[toolbar release];
}
- (void)dealloc
{
[popOverController release];
[propertiesButton release];
[downloadButton release];
[versionsButton release];
[commentaryButton release];
[webView release];
[super dealloc];
}
With NSZombieEnabled i get
'2011-08-01 10:30:36.571 ProjectName[100:707] *** -[UIBarButtonItem release]: message sent to deallocated instance 0x1fb330'
We are not sure how to fix the problem.
Thank you in advance.
You are releasing propertiesButton, downloadButton, versionsButton, commentaryButton two times. First time in
viewDidLoadand again indealloc.You don’t have to release them in
deallocas you have already released it inviewDidLoad.