views:

61

answers:

1

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?

A: 

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.

outis
Great thanks outis. I am a java programmer, hence the concepts of ownership etc dont quite flow naturally yet :)Another related question, if I create a NSArray (using [new]) of NSString, then, is it correct that I dont need to retain the NSString anymore, since it is now owned by the NSArray which is owned by me. Hence, if I retain/release my NSArray properly, the NSStrings it contains will always be implicitly retained/release by me.We assume that I require the NSArray and its member NSStrings to be available throughout the application lifecycle.
Amarsh
@Amarsh: correct about NSArray. Updated answer gives a little more detail.
outis
Thanks again outis. Another question in the same context.I add an object as follows:[someNSArray addObject:[SomeClass alloc]]and a later, call:[someNSArray removeAllObjects]In this case, will my SomeClass object get deallocated by itself?My fundamental confusion is, in cases where we alloc-ate an object and add it to a collection, who is the owner, the collection or you ... or both?
Amarsh
@Amarsh: both the collection and the object or function that allocated a SomeClass will own it. You should really read the memory management document I link to; it answers that question first thing with the Fundamental Rule of Memory Management. Also, you should always call an `init` method after calling `alloc`: `[someNSArray addObject:[[SomeClass alloc] init]]`, otherwise you'll end up with an object that's been allocated space but not filled out. The `init` methods are like constructors in Java.
outis