Reputation: 2999
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.
Upvotes: 2
Views: 4436
Reputation: 51374
You are releasing propertiesButton, downloadButton, versionsButton, commentaryButton two times. First time in viewDidLoad
and again in dealloc
.
You don't have to release them in dealloc
as you have already released it in viewDidLoad
.
Upvotes: 4
Reputation: 467
As i see it, you are releasing your buttons twice. First time in your viewDidLoad() function and finally in your dealloc function.
Upvotes: 0
Reputation: 170839
You already release your UIBarButtonItem's after you add them to array - so you must not release them again in dealloc method - those extra release calls result in sending message to already deallocated buttons and make your app crash
Upvotes: 0