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.
copycreates 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 thecopymethod 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
Personclass with the propertiesname,surnameandage, if you needed to implement thecopymethod yourself it would look sthg like this: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
copyon an object obey the same rules asretainorallocregarding the need to callreleaseorautoreleaseto balance the refcount at some pointNote that there is some exceptions / special cases, especially for some classes that are known as “immutable”, like
NSArrayorNSString. 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
NSArrayandNSStringand some other, thecopymethod may simply implemented to do a simpleretainas 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 anNSMutableArrayfrom anNSArrayfor example) do a real copy and not a simple retain, and the implementation of thecopymethod in the mutable subclasses likeNSMutableStringandNSMutableArraydo 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 aretain.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
releasethe object returned bycopy)