Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c UIToolbar内存泄漏_Objective C_Uitableview_Memory Leaks_Uitoolbar_Navigationcontroller - Fatal编程技术网

Objective c UIToolbar内存泄漏

Objective c UIToolbar内存泄漏,objective-c,uitableview,memory-leaks,uitoolbar,navigationcontroller,Objective C,Uitableview,Memory Leaks,Uitoolbar,Navigationcontroller,目前,我有一个基于导航的应用程序,显然RootViewController是一个UITableView。但是,我认为有必要创建一个浮动在UITableView上方的UIToolbar。目前我是这样做的 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; //Initialize the toolbar toolbar = [[UIToolbar alloc] init]; toolba

目前,我有一个基于导航的应用程序,显然RootViewController是一个UITableView。但是,我认为有必要创建一个浮动在UITableView上方的UIToolbar。目前我是这样做的

- (void)viewWillAppear:(BOOL)animated {

 [super viewWillAppear:animated];


 //Initialize the toolbar 
 toolbar = [[UIToolbar alloc] init]; 
 toolbar.barStyle = UIBarStyleDefault;

 //Set the toolbar to fit the width of the app. 
 [toolbar sizeToFit];

 //Caclulate the height of the toolbar 
 CGFloat toolbarHeight = [toolbar frame].size.height;

 //Get the bounds of the parent view 
 CGRect rootViewBounds = self.parentViewController.view.bounds;

 //Get the height of the parent view. 
 CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);

 //Get the width of the parent view, 
 CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);

 //Create a rectangle for the toolbar 
 CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);

 //Reposition and resize the receiver 
 [toolbar setFrame:rectArea];

 //Create a button 
 UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithTitle:@"Settings" style:UIBarButtonItemStyleBordered target:self action:@selector(account_details)];

 [toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];

 //Add the toolbar as a subview to the navigation controller. 
 [self.navigationController.view addSubview:toolbar];
 [infoButton release];

 [self.tableView reloadData];

}
然而,在使用Leaks instrument工具之后,我能够确定这是导致一些内存泄漏的原因,虽然很小,但内存泄漏仍然存在。然后,我进一步深入研究,能够准确指出导致内存泄漏的线路。它们如下

UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithTitle:@"Settings" style:UIBarButtonItemStyleBordered target:self action:@selector(account_details)];

 [toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];

 [self.navigationController.view addSubview:toolbar];

我正在努力找出如何消除这些内存泄漏,从而使我的应用程序运行更平稳。请提供任何帮助,了解上述管线导致泄漏的原因

每次视图出现时,都会创建一个新的工具栏,并添加到视图中,但从未发布。这意味着该工具栏及其工具栏按钮项将永远存在。您只需在将工具栏添加到视图后释放工具栏,或在创建工具栏时向其发送自动释放消息,即可解决此问题。因此,一个体面的方法是替换:

toolbar = [[UIToolbar alloc] init];
与:

此外,按照这样做的方式,每次视图出现时,都会向导航控制器的视图添加另一个工具栏。因此,几乎可以肯定的是,这些对象中有相当多的对象位于彼此的顶部(因此,在导航视图最终消失之前,您仍然会看到泄漏)。您可能希望将此工具栏保持为ivar。视图消失后,从导航控制器的视图中删除工具栏。当它出现时,添加它。在
viewdiload
方法中创建工具栏本身,并在
viewdiload
中清理它,然后在dealloc中释放它。因此,您的新类可能如下所示(假设您创建了一个名为toolbar的合成属性,该属性为retain):

toolbar = [[[UIToolbar alloc] init] autorelease];
- (void)viewDidLoad
{
  [super viewDidLoad];
  UIToolbar* toolbar = [[[UIToolbar alloc] init] autorelease];
  // set up toolbar
  [self setToolbar:toolbar];
  // other load code
}

- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  [[[self navigationController] view] addSubview:[self toolbar]];
  // other vwa code
}

- (void)viewDidDisappear:(BOOL)animated
{
  [super viewDidDisappear:animated];
  [[self toolbar] removeFromSuperview];
}

- (void)viewDidUnload
{
  [self setToolbar:nil];
  [super viewDidUnload];
}

- (void)dealloc
{
  UIToolbar* toolbar = [self toolbar];
  [toolbar removeFromSuperview]; // shouldn't ever need this, but be safe
  [toolbar release];
  // other dealloc
  [super dealloc];
}