OStack程序员社区-中国程序员成长平台

标题: iphone - super dealloc EXC_BAD_ACCESS 错误 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:58
标题: iphone - super dealloc EXC_BAD_ACCESS 错误

在分析我们的项目时,Xcode 在自定义 UIBarButtonItem 处带有一个 posibe 泄漏通知。 我修复了泄漏,但是在第二次加载 View 时,[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 actionselector(properties)];
    [buttons addObject:propertiesButton];
    [propertiesButton release];

    // Create a comments button
    commentaryButton = [[UIBarButtonItem alloc]
                        initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self actionselector(comments)];
    [buttons addObject:commentaryButton];
    [commentaryButton release];

    // create a versions button
    versionsButton = [[UIBarButtonItem alloc]
                      initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self actionselector(versions)];
    [buttons addObject:versionsButton];
    [versionsButton release];

    // create a save button
    downloadButton = [[UIBarButtonItem alloc]
                      initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:nil actionselector(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'

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

提前谢谢你。



Best Answer-推荐答案


您正在发布 propertiesButton、downloadButton、versionsButton、commentaryButton 两次。第一次在 viewDidLoad 中,再次在 dealloc 中。

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

关于iphone - super dealloc EXC_BAD_ACCESS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6909918/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4