Iphone 目标c使用uisegmentcontrol切换视图时内存泄漏
我有一个我不明白的内存泄漏。我使用以下分段控件切换视图:Iphone 目标c使用uisegmentcontrol切换视图时内存泄漏,iphone,objective-c,ios,memory-leaks,uisegmentedcontrol,Iphone,Objective C,Ios,Memory Leaks,Uisegmentedcontrol,我有一个我不明白的内存泄漏。我使用以下分段控件切换视图: - (void)didChangeSegmentControl:(UISegmentedControl *)control { if (self.activeViewController) { [self.activeViewController viewWillDisappear:NO]; [self.activeViewController.view removeFromSuperview];
- (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初始化方法是:
- (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;
}
我唯一能指出的是[自建底部按钮] 看看你的代码,我能看到的唯一问题是[self-BuildBottomBarButtons] 每次切换视图控制器时,似乎都在将工具栏添加到容器视图中
Instruments说什么类型的对象泄漏,是否可以看到Instruments屏幕上显示泄漏对象的屏幕截图?泄漏不在您提供的代码中,但可能是由此代码中的某些内容触发的。你能告诉我们BuildBottomBarButtons方法吗?此外,方法应该以小写字母开头,即使对于类方法也是如此。该规则的唯一例外是首字母缩写词,如仪器中的-[NSString UTF8String],可以看到泄漏可能所在的线路。你看到了吗?它指向哪一行?泄漏的行是:[self.containerView addSubview:self.activeViewController.view];我拆下了BuildBottomBarButtons的线,泄漏还在这里。现在它在同一个漏洞中有另一条管线。在viewDidLoad中,我调用了具有以下泄漏的BindViews:companyName.text=[jobDetails objectForKey:@companyName];是否可以看到泄漏块列表的屏幕,以便我们可以看到泄漏对象的类型。看看你的代码,我看不出有什么问题。我唯一能想到的建议是检查视图控制器中的代码。您是否在视图控制器上的ViewWillDisplay、ViewDidDisplay方法中执行任何操作?