As stated in Cocoa Memory Management Rules from before
You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message.
haven't read it after December 2010, but since has changed since then to
You “create” an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy).
Notice that now, it is required to have "copy" as a prefix. This resulted to a few memory related warnings from Clang Static Analyzer :(. After searching the interwebs, I haven't got to a conclusion as to why was this changed since this is one of the base foundations of Memory Management for iOS.
Does anybody know why? Thanks!
There were some methods that contained "Copy" in their text, but were clearly not copying methods. For example, +[NSData dataWithBytesNoCopy:length:]
. It was, of course, possible to use annotations for the static analyzer to inform about the nonstandard behavior, but in general I suspect that almost nobody (yourself excepted) ever wrote a copy method that didn't start with copy
or mutableCopy
, so they decided to just simplify things.
I'm glad they did, frankly, as I've run into the opposite problem, where a method contained the word "Copy" but was not intended to return an owning reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With