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 7489851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:23:12+00:00 2026-05-29T15:23:12+00:00

Inside an initialization method, I have the following code – (id)init { self =

  • 0

Inside an initialization method, I have the following code

- (id)init {

    self = [super init];

    if (self) {

        UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        tempButton.frame = CGRectMake(0,0,300,44);

        // some custom code...

        self.myButton = tempButton;
    }

    return self;
}

Where myButton is a retained property.
I know that, for what concerns memory management rules, this method equals this other:

- (id)init {

    self = [super init];

    if (self) {

        UIButton *tempButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,300,44)];

        // some custom code...

        self.myButton = tempButton;
        [tempButton release];
    }

    return self;
}

But in this case I need to use the first “version” because the buttonType property is readonly and I cannot change it after having the button initalized.

Since I find myself using the “non init-release” version in multiple methods all over my application, and for several object (most of them are NSString), my question is: not counting in this case the assignment to the property which retains the object, when the tempButton object will be released? Maybe at the end of the method/if statement? Or will the first “version” lead to an increased memory usage, since the object is not being released right away but after a certain amount of time?

  • 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-29T15:23:13+00:00Added an answer on May 29, 2026 at 3:23 pm

    I think you’re a bit confused here: in both of your snippets, you create a tempButton object, but then you’re assigning it to self.myButton. At that point, both tempButton and self.myButton are pointers to the same object. Now, presumably the myButton @property you’re using is a strong property, so by assigning tempButton to it, you increase its retain count, and therefore in either version of the code it would have a retain count of +1 at the end, and would not be dealloc’ed.

    If, hypothetically, myButton wasn’t a strong property, then there would be an error in your code, and in both cases tempButton would be prematurely released and dealloc’ed. Here’s what would happen in the two cases:

    In your first version, since you’re getting tempButton comes from something other than an init or copy method, it gets a retain count of +1, but is autoreleased. At the end of the current iteration of the run loop, the autorelease would kick in, bringing its retain count to 0 and causing it to be dealloc’ed.

    In the second version, you first get a tempButton with a retain count of 1 because it’s coming from an init method. But later on you explicitly release it, bringing its retain count to 0, at which point it is immediately dealloc’ed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Inside my template function I have the following code: TypeName myFunction() { TypeName result;
Inside my page, I have the following: <aspe:UpdatePanel runat=server ID=updatePanel> <ContentTemplate> <local:KeywordSelector runat=server ID=ksKeywords
How can i do a initialization for the Frame() while its getting used inside
I have an array declared inside a user defined method. I use this array
Inside my code I'm generating hashes of URLs, (which are practically of unbounded length).
Inside my control, I have: ContextMenu = new ContextMenu(); ContextMenu.MenuItems.Add(new MenuItem(&Add Item, onAddSpeaker)); ContextMenu.MenuItems.Add(new
I have an id<NSFastEnumeration> object. I want to count the elements inside the object.
Is it possible to get the class type from inside the static initialization block?
Is it possible to use a lambda expression inside an object initialization expression? Please
Following up from my previous question. Can anyone explain why the following code compiles

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.