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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:18:49+00:00 2026-06-12T14:18:49+00:00

I couldn’t get the right answer for this question. If it is found duplicate,

  • 0

I couldn’t get the right answer for this question. If it is found duplicate, please make it duplicate with the right reference link.

I wanted to know, what are the ways the reference count is incremented. I know, alloc/init and retain increases the reference count, does ‘copy’ also increases the reference count to ‘1’ ?

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. Editorial Team
    Editorial Team
    2026-06-12T14:18:50+00:00Added an answer on June 12, 2026 at 2:18 pm

    copy creates a new object which is, as the method name suggest, a carbon copy of the receiver (well in fact it depends of the implementation of the copy method in each class, but that’s the aim of the method anyway).

    So in fact it does not really “increase the reference count of the by 1” but it rather create a new object, with a refcount of 1 as any newly allocated object, and make it have the same property / ivar values as the original.

    So imagine you have a Person class with the properties name, surname and age, if you needed to implement the copy method yourself it would look sthg like this:

    -(id)copy
    {
        // Create a new instance
        Person* mycopy = [[Person alloc] init];
        // Make it an exact copy of the original
        mycopy.name = self.name;
        mycopy.surname = self.surname;
        mycopy.age = self.age;
        // and return that copy
        return mycopy;
    }
    

    Note that in this case, if you modify the copy later, the original is not modified, because it is a different instance.

    With this principle, the original object does not have its refcount incremented by one, but you have a new object whose refcount is one (as if you just did alloc/init to create this new object yourself) and you still have to release or autorelease it at some point yourself (if you are not using ARC). That’s why calling copy on an object obey the same rules as retain or alloc regarding the need to call release or autorelease to balance the refcount at some point


    Note that there is some exceptions / special cases, especially for some classes that are known as “immutable”, like NSArray or NSString. In such cases, it is reasonable to thingk that making a copy (aka a different object instance) that is a clone of the original, whereas the original cannot be modified, is not really efficient, and can be optimized.

    So in cases like NSArray and NSString and some other, the copy method may simply implemented to do a simple retain as the behavior will be the same as you can’t modify the original (and neither the copy) because these are immutable classes by nature.

    Of course, the implementation of mutableCopy (to get an NSMutableArray from an NSArray for example) do a real copy and not a simple retain, and the implementation of the copy method in the mutable subclasses like NSMutableString and NSMutableArray do a real copy too, but for the case that request an immutable copy of an immutable object, the point of allocating a different instance is generally useless and memory-consuming and is thus implemented the same as a retain.

    But all this probable optimization does not change neither the behavior (because the classes are immutable) nor the memory management policy (as you still need to release the object returned by copy)

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

Sidebar

Related Questions

I couldn't find a straight answer to my question and need to know it
Couldn't find an answer to this question. It must be obvious, but still. I
Couldn't seem to find the answer to this question - what is the haml
I couldn't find anything on this, but probably its just because I don't know
I couldn't find a definitive answer from the PayPal docs on this. According to
Couldn't find anything on this with a Google Search. Does anyone know how to
Couldn't find anything relevant in forums So ,Please help me with this code .I'm
This could be a duplicate question, but I have no idea what search terms
Couldn't find an answer to this solution, so once I figured it out I
Couldn't find an answer to this one. I have a WPF ListView control that

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.