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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:43:55+00:00 2026-06-13T00:43:55+00:00

I am doing some research on reference count increase. Please help on finding it.

  • 0

I am doing some research on reference count increase. Please help on finding it.
Below is sample code and research i’m doing what would happen of reference counting for each line below.

.h file

NSArray *tempArray;
@property (nonatomic, retain) NSArray *tempArray;

.m file

@synthesize tempArray;

    -(void) sampleFunction
{
    NSArray *myArray = [[NSArray alloc] init]; // Thinking reference count increases to "1"
    tempArray = myArray;// reference count increases and tempArray gets retain count "1" now.
    tempArray = myArray;// reference count increases and tempArray gets retain count "2" now.
    tempArray = [NSArray arrayWithObject:@"SomeString"]; // retain count = ?

}

I know this code may not be for functioning, but this is for only researching about what will happen on reference counting for such scenarios. I tried printing retainCount, but it doesn’t show the correct result. Please advise me how does the reference count works on this each line?

  • 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-06-13T00:43:56+00:00Added an answer on June 13, 2026 at 12:43 am

    In lines 2, 3 and 4 you are affecting the instance variable tempArray to the same object as myArray. But if you write it this way, you try to affect an instance variable. As a matter of fact, if you didn’t write any @synthesize tempArray or @synthesize tempArray = tempArray in your code, by default the instance variable generated automatically to store the property value is the same name as the property name, but prefixed with an underscore. So as the property name is tempArray, the instance variable is named _tempArray. The instance variable tempArray itself does not exist and your line of code is invalid.

    So if we suppose you wrote instead:

    -(void) sampleFunction
    {
      NSArray *myArray = [[NSArray alloc] init]; // (1)
      self.tempArray = myArray; // (2)
      self.tempArray = myArray; // (3)
      self.tempArray = [NSArray arrayWithObject:@"SomeString"]; // (4)
    }
    
    • In (1) you are creating a brand new instance of NSArray. “alloc” always initialize new instance with a reference count of 1
    • In (2) you write self.tempArray = myArray (which is equivalent to [self setTempArray:myArray]; and thus call the property setter), so you set the property to point to the same array you created in (1). This array is thus retained by the property, and its retainCount increses by one, because it is retained by myArray and by the self.tempArray property.
    • In (3) you affect the property to the very same object as before. This the ref count does not change at all. You could understand that as if you replaced the value of the self.tempArray with another value, so the setter of the property release the old value (decrementing its ref count), then retain the new value (thus incrementing its ref count). As in your case the old and new values are the same object, you would decrement the ref count of your array then re-increment it again. In practice, the ref count does not even change at all (instead of decrementing+incrementing again) to avoid any potential dealloc of the object, because the default implementation of a property setter is as follow:

      -(void)setTempArray:(NSArray*)newValue
      {
        // check if old and new value are different. Only do sthg if they are different
        if (newValue != _tempArray)
        {
          [_tempArray release];  // release old value
          [newValue retain];     // retain new value
          _tempArray = newValue; // store new value in the backing variable associated with the property
        }
      }
      
    • In (4) you replace again the value of the property tempArray, but this time with a completely new object. So the property will release its old value and retain the new one. Thus the first array you created in (1) which had a refcount of 2 (retained by myArray and by self.tempArray) decrease its ref count to 1 (because the property won’t retain it anymore), and the new instance you created [NSArray arrayWithObject:@"SomeString"] is retained by the property, so its ref count is +1.


    If you replaced self.tempArray = ... (so the use of the property) with the direct use of the instance variable, using instance variables don’t retain the objects they are affected to (except if you are using ARC but it seems you don’t), so the ref count of the object wouldn’t have changed at all in (2), (3) and (4).

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

Sidebar

Related Questions

After doing some research, it would seem that errno 150 occurs when either table
I am just doing some research into image processing and would appreciate it if
After doing some research on the subject, I've been experimenting a lot with patterns
Im doing some research for an upcoming project and I am wondering if it
I'm doing some research and I'm playing with Apache Mahout 0.6 My purpose is
I am doing some research on developing thread safe applications. I know users can
In asking around and doing some research, it seems like many people are really
I've been doing some research about Swing in order to build a css editor
I am doing some research into geolocation in web pages. I have set up
I have been doing some research on test driven development and find it pretty

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.