When you add an object to an array, it calls retain
on that object. If you don't release your pointer to that object, it will be a leak. When you release the array, it will call release
on all of the objects that it holds, since it called retain
previously.
As for autorelease vs release, the only way to know for sure (aside from possibly reading the documentation) is by the name of the method. I believe the rule in general is that if you didn't allocate the object, then you aren't responsible for releasing it.
Regarding the object creation methods, all of the convenience methods (array:
, arrayWithObjects:
, arrayWithArray:
, etc.) return autoreleased objects. However, their corresponding init methods (init:
, initWithObjects:
, initWithArray:
, etc.) do not - if you call them, you are responsible for calling release
on the returned object.
I seem to recall a few other questions on this topic here - you might try searching around for a more thorough explanation.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…