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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:19:38+00:00 2026-05-11T08:19:38+00:00

I see a lot of code, particularly in Apple example code, that resembles the

  • 0

I see a lot of code, particularly in Apple example code, that resembles the following:

   EditingViewController *controller = [[EditingViewController alloc] initWithNibName:@'EditingView' bundle:nil];     self.editingViewController = controller;     [controller release]; 

Is there any reason in particular that the approach above proves beneficial over:

self.editingViewController = [[EditingViewController alloc] initWithNibName:@'EditingView' bundle:nil]; 

Trying to understand if there is a strategy for the above.

Thanks!

  • 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. 2026-05-11T08:19:39+00:00Added an answer on May 11, 2026 at 8:19 am

    On first glance, it would seem that your example would work, but in fact it creates a memory leak.

    By convention in Cocoa and Cocoa-touch, any object created using [[SomeClass alloc] initX] or [SomeClass newX] is created with a retain count of one. You are responsible for calling [someClassInstance release] when you’re done with your new instance, typically in your dealloc method.

    Where this gets tricky is when you assign your new object to a property instead of an instance variable. Most properties are defined as retain or copy, which means they either increment the object’s retain count when set, or make a copy of the object, leaving the original untouched.

    In your example, you probably have this in your .h file:

    @property (retain) EditingViewController *editingViewController; 

    So in your first example:

    EditingViewController *controller =      [[EditingViewController alloc] initWithNibName:@'EditingView' bundle:nil]; // (1) new object created with retain count of 1  self.editingViewController = controller; // (2) equivalent to [self setEditingViewController: controller]; // increments retain count to 2  [controller release]; // (3) decrements retain count to 1 

    But for your second example:

    // (2) property setter increments retain count to 2 self.editingViewController =       // (1) new object created with retain count of 1     [[EditingViewController alloc] initWithNibName:@'EditingView' bundle:nil];  // oops! retain count is now 2 

    By calling the autorelease method on your new object before passing it to the setter, you ask the autorelease pool to take ownership of the object and release it some time in the future, so for a while the object has two owners to match its retain count and everything is hunky dory.

    // (3) property setter increments retain count to 2 self.editingViewController =       // (1) new object created with retain count of 1     [[[EditingViewController alloc] initWithNibName:@'EditingView' bundle:nil]          // (2) give ownership to autorelease pool          autorelease];  // okay, retain count is 2 with 2 owners (self and autorelease pool) 

    Another option would be to assign the new object directly to the instance variable instead of the property setter. Assuming your code named the underlying instance variable editingViewController:

    // (2) assignment to an instance variable doesn't change retain count editingViewController =       // (1) new object created with retain count of 1     [[EditingViewController alloc] initWithNibName:@'EditingView' bundle:nil];  // yay! retain count is 1 

    That’s a subtle but critical difference in the code. In these examples, self.editingViewController = x is syntactic sugar for [self setEditingViewController: x] but editingViewController is a plain old instance variable without any retain or copy code generated by the compiler.

    See also Why does this create a memory leak (iPhone)?

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

Sidebar

Ask A Question

Stats

  • Questions 61k
  • Answers 61k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I recently bumped into the jQuery Form Example plugin that… May 11, 2026 at 9:38 am
  • added an answer I do [2], but one problem I have is that… May 11, 2026 at 9:38 am
  • added an answer Because of the problems raised in this thread, I'm posting… May 11, 2026 at 9:38 am

Related Questions

I see a lot of code like this: function Base() {} function Sub() {}
I see a lot of example code for C# classes that does this: public
I see a lot of talk on here about functional languages and stuff. Why
I see a lot of IoC frameworks for .Net and Java. Does anyone know
I am looking into captive portals for my organization. I see a lot of
Just as the title says, I see a lot of editors touting macro recording
Looking through my server logs, I see that a lot of pages on my
I see on the web there are a lot of questions about caching ASP.Net,
It's one of those things I see a lot but never really think of.
Where do you get Microsoft.Web.Mvc.dll? I see it included in a lot of ASP.NET

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.