Question

If I create a String with [NSString StringWithFormat], do I have to [retain] it? My understanding is that convenience methods add the objects to autorelease pool. If that is the case, shouldn't we retain the object so that it doesn't get drained with pool at the end of the event loop?

Was it helpful?

Solution

If the string should persist beyond the current function, then yes, the object that needs it should retain it. Here's one of those places where the English phrasing seems trivial but is actually concealing a key concept. Rather than saying "do I have to retain it?" or (as I originally wrote) "if you want the string to persist," talk in terms of an object retaining the string. The difference is the latter expresses the concept of ownership (note: the Apple documentation uses "you" when talking of ownership, but it's a third-person neuter "you" rather than a second-person "you"). Of course, if you're storing the string in a property, then retaining (or copying) is handled for you. Unless you need to target older versions of OS X (10.4 and earlier), you should generally be using properties.

The standard collections own the objects they contain, and thus retain them. When an element is removed from a collection, the collection releases the element (note: they don't autorelease, so if the object has no other owners, the object will be destroyed). Read more about this topic in "Validity of Shared Objects". Apple's memory management documentation should tell you everything you need to know.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top