超级dealloc EXC_BAD_ACCESS错误

时间:2011-08-02 09:30:56

标签: iphone objective-c ios ipad memory-management

在分析我们的项目时,Xcode在自定义UIBarButtonItem中附带了一个posibe泄漏通知。 我修复了泄漏问题,但是第二次加载视图时,[super dealloc]会出现EXC_BAD_ACCESS错误。

从UIBarButtonItem中删除自动释放(因此返回警告):

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:toolbar] autorelease];
重新加载屏幕时

没有问题。

自定义UIBarButtonItem和dealloc代码:

- (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];
}

使用NSZombieEnabled我得到

'2011-08-01 10:30:36.571 ProjectName[100:707] *** -[UIBarButtonItem release]: message sent to deallocated instance 0x1fb330'

我们不确定如何解决问题。

提前谢谢。

3 个答案:

答案 0 :(得分:4)

你发布了两次propertiesButton,downloadButton,versionsButton,commentaryButton。第一次在viewDidLoad,第一次在dealloc

您不必在dealloc中发布它们,因为您已在viewDidLoad中发布了它。

答案 1 :(得分:0)

在将UIBarButtonItem添加到数组后已经释放了它们 - 所以必须不能在dealloc方法中再次释放它们 - 这些额外的释放调用会导致向已经解除分配的按钮发送消息并使应用程序崩溃

答案 2 :(得分:0)

正如我所看到的,你正在释放按钮两次。第一次在viewDidLoad()函数中,最后在你的dealloc函数中。