Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3348474
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:32:49+00:00 2026-05-18T01:32:49+00:00

Currently I have a navigation based application and obviously the RootViewController is a UITableView.

  • 0

Currently I have a navigation based application and obviously the RootViewController is a UITableView. However, I deemed it necessary to create a UIToolbar that floats above the UITableView. Currently I do this like this.

- (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];

}

However, after using the Leaks instrument tool, I was able to determine that this was the cause for a few memory leaks, only small, but memory leaks nonetheless. I then drilled down even further and was able to pin point the exact lines that are causing the memory leaks. They are the following.

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];

I am struggling to figure out how to remove these memory leaks and thus causing my application to run smoother. Any help would be appreciated as to why the above lines are causing leaks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:32:50+00:00Added an answer on May 18, 2026 at 1:32 am

    A new toolbar is created every time the view appears, added to the view and never released. This means that both that tool bar and its bar button item will last forever. You can fix this by simply releasing the toolbar after you add it to the view, or sending it the autorelease message when you create it. So, a decent way to do this would be to replace:

    toolbar = [[UIToolbar alloc] init];
    

    with:

    toolbar = [[[UIToolbar alloc] init] autorelease];
    

    Also, the way you’re doing this, every time your view appears you end up adding another toolbar to the navigation controller’s view. So you almost certainly have quite a few of these objects sitting on top of each other (so you will still see leaks until the navigation view finally goes away). What you might want to do is keep this toolbar as an ivar. When your view disappears, remove the toolbar from the nav controller’s view. When it appears, add it. Create the toolbar itself in your viewDidLoad method and clean it up in viewDidUnload then release it in dealloc. So your new class might look like this (let’s assume you create a synthesized property named toolbar that’s retain):

    - (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];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have an MS Access application that connects to a PostgreSQL database via
I currently have a DetailsView in ASP.NET that gets data from the database based
I have a navigation bar based application, and at one specific point in the
I have a navigation based application where I place my custom views in the
I currently have heavily multi-threaded server application, and I'm shopping around for a good
I currently have a class and I'm trying to create an easy GUI to
I currently have a functioning in-house Windows Forms application which extensively uses the DataGridView
I am currently in the process of performance tuning a Web Application and have
I currently have my navigation controllers defined in my appDelegate as followed (code summarized):
I currently have speakers set up both in my office and in my living

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.