2011-08-02 47 views
2

分析我們的項目Xcode時,在定製的UIBarButtonItem中帶有posibe泄漏通知。 我修正了漏洞,但第二次加載視圖時,[super dealloc]給出了一個EXC_BAD_ACCESS錯誤。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' 

我們不知道如何解決這個問題。

預先感謝您。

回答

4

您正在發佈propertiesButton,downloadButton,versionsButton,com.Button兩次。第一次在viewDidLoad,再次在dealloc

您不必在dealloc中發佈它們,因爲您已經在viewDidLoad中發佈了它們。

+0

非常感謝!這是一個例程,當我創建一個屬性時,我將它們添加到dealloc方法中。但是當我通過另一個物體時,我也會釋放它們。仍然奇怪的是,xcode告訴我問題出在[super dealloc]上。再次感謝1 – Justin

+0

歡迎! Juzzz! – EmptyStack

0

將它們添加到陣列後,您已經釋放你的UIBarButtonItem的 - 所以你不得在dealloc方法再次釋放出來 - 這些額外的呼籲釋放導致發送消息已經釋放按鈕,使您的應用程序崩潰

0

當我看到它時,你會釋放你的按鈕兩次。第一次在你的viewDidLoad()函數中,最後在你的dealloc函數中。