使用分段控件切换视图时,目标c内存泄漏

时间:2011-12-20 08:24:16

标签: iphone objective-c ios memory-leaks uisegmentedcontrol

我有一个我不明白的内存泄漏。我正在使用像这样的segmentcontrol切换视图:

- (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 个答案:

答案 0 :(得分:0)

我唯一可以指出的是[self BuildBottomBarButtons];

答案 1 :(得分:0)

查看代码我能看到的唯一问题是[self BuildBottomBarButtons]

每次切换视图控制器时,看起来都是将工具栏添加到容器视图中。

乐器说什么类型的物体泄漏,是否可以看到仪器屏幕的屏幕截图显示泄漏的物体?