【问题标题】:super dealloc EXC_BAD_ACCESS errorsuper dealloc EXC_BAD_ACCESS 错误
【发布时间】:2011-10-18 02:29:50
【问题描述】:

在分析我们的项目时,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'

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

提前谢谢你。

【问题讨论】:

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


    【解决方案1】:

    您将发布 propertiesButton、downloadButton、versionsButton、commentaryButton 两次。第一次在viewDidLoad,然后又在dealloc

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

    【讨论】:

    • 非常感谢!这是一个例程,当我创建一个属性时,我将它们添加到 dealloc 方法中。但是当我通过另一个对象传递它时,我也会释放它们。 xcode 告诉我问题出在 [super dealloc] 仍然很奇怪。再次感谢1
    【解决方案2】:

    在我看来,您释放了两次按钮。第一次是在你的 viewDidLoad() 函数中,最后是在你的 dealloc 函数中。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-13
        • 1970-01-01
        • 2012-08-08
        • 1970-01-01
        • 2014-10-12
        • 1970-01-01
        相关资源
        最近更新 更多