Say I have an NSMutableArray *sasquatch that has some arrays in it.
In ARC, if I call [sasquatch removeAllObjects]; does this cause all the arrays in the sasquatch array to be nil? So that the objects in the arrays of the sasquatch array are also all removed too and dealloced and stuff?
Because I don’t know if I should do this:
for (NSArray *animalPlanetSearchingFor in sasquatch){
[animalPlanetSearchingFor removeAllObjects];
}
before I call [sasquatch removeAllObjects];
it won’t recursively empty all arrays — it will decrement their ref counts when removed. when the ref count of an array is 0, all objects are removed. they clean up after themselves.
iow,
sasquatch‘s sub-arrays are not changed — with one exception: ifsasquatchheld the last reference to that sub-array. in that case, the element (sasquatch’s sub-array) will be deallocated.Well, it just sends
releaseto each element an changes thecount(etc.).NSArrayscan’t holdnilelements — it is forbidden.So if you make a
copyof the array – then a reference to the sub-arrays will be added, and you would extend the sub-arrays’ lifetimes because a reference is now held by the copy as well.The sub-arrays will be deallocated at that time if sasquatch held the last reference. Remember that references may also exist in autorelease pools so your order of destruction is not explicitly defined — even in the simplest case.
Another way to look at it: Everything in
sasquatchand its sub-arrays (recursively) will be destroyed when the last reference of it is lost — only if every element is the last held reference.In short — there is no need to call
removeAllElementsin either case for the purpose of cleanup.