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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:55:53+00:00 2026-05-22T11:55:53+00:00

I see in some sample code that autorelease is used. I am not familiar

  • 0

I see in some sample code that autorelease is used. I am not familiar with the instances when this is required. For example, if I create an annotation object

Header file

@interface someViewController: UIViewController 
{
    Annotation *annotation;
}

@property (nonatomic, retain) Annotation *annotation;

@end

Implementation file

@implementation someViewController
@synthesize annotation
@end

Question: Is it the correct approach if I initialize my annotation object in the implementation file like this?

self.annotation = [[Annotation alloc] initWithCoordinate:location];

Do I need to set autorelease for this? Or can I just do it the normal way and add the release in the dealloc method?

  • 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-22T11:55:54+00:00Added an answer on May 22, 2026 at 11:55 am

    this is correct:

    self.annotation = [[[Annotation alloc] initWithCoordinate:location] autorelease];

    because annotation property is declared as a retain property, so assigning to it will increment its retain count.

    you will also need, all the same, to release self.annotation in -dealloc.

    in short:

    1. init will set retain count to 1;

    2. assigning to self.annotation, will set it to 2;

    3. autorelease will set it back to 1 when the main loop is executed again;

    4. release in dealloc will set the retain count to 0, so that the object will be deallocated);

    the best way to think of autorelease is the following, in my opinion: autorelease will “schedule” an “automatic” release for your object at some (near) point in future (typically when the control flow goes back to the main loop, but details are hidden in the hands of Apple).

    autorelease is mostly useful in conjunction with init, specifically in the following cases:

    1. when you init a local variable, so that you don’t have to release it explicitly before it goes out of scope (the main loop will do that for you);

    2. when you return a pointer to an object you have just created without keeping ownership of it (typical case of the create/make* kind of selectors, the receiver is required to retain it to get ownership);

    3. with properties that retain, when you assign to them an object that they should own uniquely;

    4. with data structures that increment the retain count (NSMutableArray, NSMutableDictionary, etc): you should generally autorelease a newly inited object when you add it to such data structure.

    apart from case 2, it is evident that the use of autorelease is meant to improve readability of the code and reduce the potential for errors (meaning that in all of the other cases, you could simply release explicitly your object after the assignment or at the end of the scope).

    when using properties, you have always to check whether they are of the retain or assign/copy case; in the first case, assigning a newly inited object to a property generally requires autorelease.

    Anyway, I would suggest at least skimming one of the many tutorial on memory management for iOS.

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

Sidebar

Related Questions

I'd like to see some sample code for that. Any good Tutorial-Link is also
Looking at some code I'm maintaining in System Verilog I see some signals that
For some reason I never see this done. Is there a reason why not?
I look around and see some great snippets of code for defining rules, validation,
I saw this the other day (scroll all the way down to see some
I have an Interface called IStep that can do some computation (See Execution in
I have some code that loads an image file off the web and puts
I can see some options available: 1) Use PEAR's POP3 class --> tried it,
EDIT: I would really like to see some general discussion about the formats, their
In the C / Unix environment I work in, I see some developers using

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.