2011-12-20 70 views
0

我有一個內存泄漏,我不明白。我切換視圖與這樣的segmentcontrol:使用uisegmentcontrol切換視圖時的目標c內存泄漏

- (void)didChangeSegmentControl:(UISegmentedControl *)control { 
    if (self.activeViewController) { 
     [self.activeViewController viewWillDisappear:NO]; 
     [self.activeViewController.view removeFromSuperview]; 
     [self.activeViewController viewDidDisappear:NO]; 
    } 

    self.activeViewController = [self.segmentedViewControllers objectAtIndex:control.selectedSegmentIndex]; 


    [self.activeViewController viewWillAppear:NO]; 
    [self.containerView addSubview:self.activeViewController.view];//Here is the memory leak 
    [self.activeViewController viewDidAppear:NO]; 

    [self BuildBottomBarButtons]; 

} 

我泄漏出現在儀器和我沒有任何想法,爲什麼... 謝謝!

編輯: 的 「BuildBottomBarButtons」:

-(void) BuildBottomBarButtons{ 


    //create toolbar using new 
    UIToolbar *toolbar = [UIToolbar new]; 
    toolbar.barStyle = UIBarStyleBlackTranslucent; 
    [toolbar sizeToFit];  
    toolbar.frame = CGRectMake(0, 330, 320, 50); 

    //Add buttons  
    UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction                     target:self                      action:@selector(shareClicked)]; 

    UIBarButtonItem *systemItem2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"scroll left.PNG"] style:UIBarButtonItemStylePlain target:self action:@selector(upClicked)]; 

    UIBarButtonItem *systemItem3 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"scroll right.PNG"] style:UIBarButtonItemStylePlain target:self action:@selector(downClicked)]; 

    //Use this to put space in between your toolbox buttons 
    UIBarButtonItem *fixItem50 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil  action:nil]; 
    UIBarButtonItem *fixItem70 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil  action:nil]; 
    fixItem50.width = 50; 
    fixItem70.width = 64; 
    //Add buttons to the array  
    NSArray *items = [NSArray arrayWithObjects: systemItem1, fixItem70, systemItem2, fixItem50,systemItem3, nil]; 

    //release buttons 
    [systemItem1 release]; 
    [systemItem2 release]; 
    [systemItem3 release]; 
    [fixItem70 release]; 
    [fixItem50 release]; 

    //add array of buttons to toolbar 
    [toolbar setItems:items animated:NO]; 

    [self.containerView addSubview:toolbar]; 
    [toolbar release]; 
} 

與 「self.segmentedViewControllers」 init()方法是:

- (NSMutableArray *)segmentedViewControllerContent { 
    JobGeneralDetailsController * controller1 = [[JobGeneralDetailsController alloc] initWithSelectedRowID:selectedRowID andWithJobBoardID:jobBoardId andWithJobDetails:jobDetails]; 
    [controller1 setViewType:jobDetailsViewType]; 
    //initWithParentViewController:self]; 
    JobMapDetailsController * controller2 = [[JobMapDetailsController alloc] initWithJobDetails:jobDetails];//[[AustraliaViewController alloc] initWithParentViewController:self]; 

    [controller1 setJobMapDetailsController: controller2]; 


    JobReviewsController *controller3 = [[JobReviewsController alloc] initWithStyle:UITableViewStyleGrouped]; 

    [controller3 setJobDetails:jobDetails]; 
    [controller3 setViewType:jobDetailsViewType]; 

    [controller1 setJobReviewsController: controller3]; 

    NSMutableArray * controllers = [NSMutableArray arrayWithObjects:controller1, controller2, controller3,nil]; 

    [controller1 release]; 
    [controller2 release]; 
    [controller3 release]; 

    return controllers; 
} 

enter image description here

+2

泄漏不在您提供的代碼中,但可能由此代碼中的某些內容觸發。你可以向我們展示'BuildBottomBarButtons'方法嗎?此外,即使對於類方法,方法也應以小寫字母開頭。這個規則唯一的例外是首字母縮略詞,例如' - [NSString UTF8String]' – dreamlax

+0

在儀器中,可能會看到泄漏線可能位於的位置。你看見了嗎?它指向哪條線? –

+0

泄漏的行是:[self.containerView addSubview:self.activeViewController.view]; –

回答

0

我可以指向唯一的行[self BuildBottomBarButtons];在你的代碼

0

尋找我能看到的唯一的問題是在[自BuildBottomBarButtons]

它看起來像要添加工具欄切換視圖控制器容器視圖每次。

儀器說什麼類型的對象泄漏,是否可以看到屏幕截圖顯示泄漏的對象?

+0

我刪除了「BuildBottomBarButtons」這一行,並且泄漏仍在此處。現在它有另一行(在同一個泄漏中)。在viewDidLoad我cal的「BindViews」其中有以下泄漏:'companyName.text = [jobDetails objectForKey:@「CompanyName」];' –

+0

嗨 - 我剛加入泄漏 –

+0

是否有可能看到泄漏列表的屏幕因此我們可以看到物體的泄漏類型。看着你的代碼,我真的不能看到任何錯誤。只有我能想到的建議是檢查視圖控制器中的代碼。你在視圖控制器的viewWillAppear,viewDidAppear方法中做了什麼? –