views:

78

answers:

1

I am too dumb to know the answer to this one - does anyone know the answer definitively?

NSMutableArray *happyDictionaryEntries;
@property (nonatomic, retain) NSMutableArray *happyDictionaryEntries;

-(void) getStuffFromServer
 {
 ..
 self.happyDictionaryEntries = [NSMutableArray
  arrayWithContentsOfURL:[NSURL URLWithString:aUrl]];
 }

Later we will release the array. I want to actually release all the elements of the array.

So, we would [happyDictionaryEntries release].

(Or I suppose just self.happyDictionaryEntries = nil since it's a property.)

BUT do you also have to [happyDictionaryEntries removeAllObjects] ?

AND/OR do you have to release each item in the array manually?

(Indeed - associated question - does anyone know if removeAllObjects in fact releases each object? It's not really clear what the situation is.) Is there an idiom?

I can manage memory no problem in a typical array that you build up "yourself", but I'm too dumb to understand what the situation is when you have arrayWithContentsOfURL in the mix. If anyone definitively knows the answer, thanks!

"The documentation for NSArray & NSMutableArray state the memory management behavior - Joshua"

Correct. Do you have any documentation for removeAllObjects or for arrayWithContentsOfURL? Can anyone decisively state what removeAllObjects does? Thanks.

"From Apple's Memory Management Programming Guide: [well-known quite form the doco]"-jlehr.

Correct, but what does removeAllObjects do? Does anyone know?

NOTE THAT there seems to be NO documentation on that. The doc on NSMutableDisctionary removeAllObjects for example IS explicit. BUT the doc on NSMutableArray removeAllObjects noticably SAYS NOTHING. It could be like (to make one example) imageNamed which basically doesn't work and does not work as one would perhaps expect.

+2  A: 

Yes, NSArray, NSSet and NSDictionary classes all retain the objects held in them. When the NSArray, NSSet or NSDictionary object's retain count reaches zero, the retained objects are all released as part of the dealloc. removeAllObjects will also release all the held objects.

John Franklin
John, thanks - to be crystal clear, in the case of an array created using +(id)arrayWithContentsOfURL, and you can see how I am assigning it to my synthesized property ... what you say is just so? Thanks!
Joe Blow
And for completeness, I am curious, how do you know that removeAllObjects releases each held object? I am damned if I could find that explicitly explained anywhere in the docu you know. Very best and thanks again.
Joe Blow
Joshua Nozzi
From Apple's Memory Management Programming Guide: "When you add an object to a collection such as an array, dictionary, or set, the collection takes ownership of it. The collection will relinquish ownership when the object is removed from the collection or when the collection is itself released." Here, the word 'relinquish' implies that a `-release` message is sent to the object.
jlehr
See edits to original question.
Joe Blow
`removeAllObjects` sends a release to each object and removes it from the collection.
John Franklin
John, fair enough, that's what I assume also. But there seems to be NO documentation on that. The doc on NSMutableDisctionary removeALlObjects IS explicit, but the doc on NSMutableArray removeAllObjects SAYS NOTHING. It could be like (to make an example) imageNamed which basically doesn't work and does not work as one would perhaps expect.
Joe Blow
At the bottom of the documentation is a link to provide feedback to Apple. I suggest you send them a note asking them to clarify the documentation.
John Franklin
Thanks John, I just wanted to check that you didn't know about some secret doco that I had never seen! Thanks again
Joe Blow