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

  • SEARCH
  • Home
  • 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 6342825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:19:07+00:00 2026-05-24T20:19:07+00:00

I am really new to objc and I am trying to understand as much

  • 0

I am really new to objc and I am trying to understand as much as possible and get a good routine when it comes to mem management.

My question is if code like this is dangerous (i love short code)

NSMutableArray *items = [[NSMutableArray alloc] init]; 

[items addObject:[[UIBarButtonItem alloc] 
                   initWithTitle:@"Login"
                   style:UIBarButtonItemStyleBordered
                   target:self 
                   action:@selector(tryUserInput)]];

[self.toolbar setItems:items animated:TRUE];
[self.view addSubview:self.toolbar];

[items release];

In the examples I can find people always create the object that they add in the array, add it and then release it. If I alloc it and adds it at the same time, the array will take care of it aye? And I am releasing that when I’m done with it. Also, can I write it like this?

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:@”Logout”
style:UIBarButtonItemStyleDone
target:nil
action:nil];

Or should I attach an autorelease on that one?

If I understood it correclty, since the “navigationitem” is a property it retains the object and takes care of it. And the array retains all the objects I add to it. So everything should be fine?

Thanks for your help

  • 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-24T20:19:07+00:00Added an answer on May 24, 2026 at 8:19 pm

    You need an to send an autorelease to the UIBarButton, or you’ll have a leak.

    When you alloc it, it has a “retain count” of +1; when you add it to the array it goes to +2. You need it to go back to +1, so that the only owner will be the array, and the UIBarButton will be deallocated when the array is freed. You can do it in two ways:

    [items addObject:[[[UIBarButtonItem alloc] 
                   initWithTitle:@"Login"
                   style:UIBarButtonItemStyleBordered
                   target:self 
                   action:@selector(tryUserInput)] autorelease]];
    

    or

    UIBarButtonItem *item = [[UIBarButtonItem alloc] 
                   initWithTitle:@"Login"
                   style:UIBarButtonItemStyleBordered
                   target:self 
                   action:@selector(tryUserInput)];
    [items addObject:item];
    [item release];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm really new to OOP and C# and I'm trying to understand, how I
I really new to this git and trying to get it work with xcode4.
im really new to linq-to-SQL so this may sound like a really dumb question,
I'm really new to VSTO so sorry if this is a newbie question. I'm
I'm really new to Python. I'm trying to import a third party module called
I'm really new to ASP.NET MVC, and I'm trying to integrate some Javascript into
Really new to kinect. I am trying to read up material I find. I
Im really new to Grails and I try to understand how it works. I
Im really new to LINQ so I'm trying to figure out how to execute
I'm really new to iOS development and I'm trying to create a simple table

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.