does copy/mutableCopy operation increment retain count value ?? (Objective C)
views:
80answers:
1
+5
A:
The normal semantics of copy
and mutableCopy
are to give you back a retained object, just like creating a new one via alloc
/init
would have. They don't increment the reference count of the object being copied, if that's what you're asking. From the NSObject
documentation for copy
:
If you are using managed memory (not garbage collection), this method retains the new object before returning it. The invoker of the method, however, is responsible for releasing the returned object.
Carl Norum
2010-09-13 17:09:15
+1, except that under some circumstances (`copy`-ing an immutable object, for example), invoking `copy` may just increment the retain count and return the same object. (however, the same memory management rules still apply, of course)
Dave DeLong
2010-09-13 17:11:09
+1 @Dave, absolutely true. There's no practical difference to the caller, though.
Carl Norum
2010-09-13 17:20:45
Most importantly, the precise retain count is an implementation detail that the programmer should not be thinking about. When you `copy`, you can treat that as a new object with a retain count of 1 in any correct program. The actual retain count is essentially none of our business.
Chuck
2010-09-13 22:13:52