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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:12:27+00:00 2026-05-22T23:12:27+00:00

Well, i am still confused about objective c properties and instance variables. I create

  • 0

Well, i am still confused about objective c properties and instance variables.
I create a LocationManager-object in my viewDidLoad. On the one hand the LocationMan is just an instance variable on the other hand it is declared as a property. Have a look at the examples:

First example:

Header:

CLLocationManager* _locationMan;

Implementation:

CLLocationManager* theManager = [[[CLLocationManager alloc] init] autorelease];
_locationMan = theManager;
_locationMan.delegate = self;

Second Example

Header:

CLLocationManager* _locationMan;
@property (retain, nonatomic) CLLocationManager* locationMan;

Implementation:

self.locationMan = [[[CLLocationManager alloc] init] autorelease];
self.locationMan.delegate = self;

Whats the difference between those examples except of that the second one is working and the first one not? What’s going on there with the memory management?

  • 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-22T23:12:27+00:00Added an answer on May 22, 2026 at 11:12 pm

    The problem you have in your first example:

    CLLocationManager* theManager = [[[CLLocationManager alloc] init] autorelease];
    

    is caused by the use of autorelease. autorelease can be seen as meaning: at some point in the near future, release automatically this object. Given the way autorelease is implemented, by means of a release pool, this usually happens the next time the control flow return to the main loop and the release pool is emptied.

    So, in your first case, you are creating the object and storing it in your ivar; but soon it will be released, and since you don’t have explicitly retained it anywhere else, it will end up being deallocated. When you access it after that, you get the error. If you had not used autorelease, everything would have worked correctly:

    CLLocationManager* theManager = [[CLLocationManager alloc] init]; //-- this is correct
    _locationMan = theManager;  //-- because you assign directly to the ivar
    

    In your second example, creation is just the same, meaning, the object will also be flagged for autorelease. But, in this case, you assign it to a property which has got the retain modifier. This means that the object will be automatically retained when assigning to the property. So, when the autorelease is actually done (when you get back to the main look, roughly), your object will have already had its retain count incremented by 1; then auto releasing it will not make its retain count go to 0, and the object will not be deallocated.

    What you have to know clearly is that:

    1. alloc will set the retain count to 1;

    2. retain properties will increment the retain count when assigning to them;

    3. autorelease is like a delayed release, so that in the mean time (before the release is actually done, which means in the rest of your method and callers up to the main loop) you can use the object safely and thereafter it will be released.

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

Sidebar

Related Questions

I'm confused about getting Qt working with Visual Studio Express. Most of the searches
I am new to class development in PHP, however I am still quite confused
I have looked at most threads here about Doubly linked lists but still unclear
My company is trying to make an informed decision about how to pursue future
I often get confused with the href attribute, the link tag, and the base
I'm a bit confused concerning proper usage of C++ namespaces. It is clear for
I want to create a matrix representation for a graph algorithm using the least
I am starting to need to work on bigger sites and with multiple people
Consider Martin Fowler's Patterns Of Enterprise Application Architecture, and the pattern of Front Controller:
I'm new to Rails and trying to understand how we build 'complex' Models (and

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.