views:

407

answers:

4

When Objective C containers are dealloc'd, do they release their references to the objects they contain or do I need to do that manually?

+3  A: 

Should have read the docs for NSArray closer:

Arrays maintain strong references to their contents—in a managed memory environment, each object receives a retain message before its id is added to the array and a release message when it is removed from the array or when the array is deallocated. If you want a collection with different object ownership semantics, consider using CFArray Reference, NSPointerArray, or NSHashTable instead.

Travis Jensen
A: 

They release their references to the objects that they contain.

DasBoot
A: 

When you add an object its reference count is incremented. When it's removed (wither manually or when the array is destroyed) its reference count is decremented.

So with the following code you would not have to release the object

NSObject* someObject = [[[SomeClass alloc] init] autorelease];
[someArray addObject: someObject];
Andrew Grant