Is "removeAllObjects" supposed to release?
I decided to start using removeAllObjects
so that I could re-use some of my NSMutableArrays. However, I am finding that the c开发者_StackOverflowall to removeAllObjects
causes the array to get released and so it crashes when the viewController is displayed for the second time.
I've looked on the web and it seems that some people are saying that removeAllObjects
just clears their array, while others are saying that it also releases the array. Which is true? It appears to release for me, but I find that weird and would expect the behaviour to be just to release the objects within the array. Nothing in the documentation warns of this, which I also find strange.
EDIT: With NSZombies turned on, all I get back is:
- -[__NSArrayM removeAllObjects]: message sent to deallocated instance 0x625e4e0
Basically telling me that because the array was released due to the removeAllObjects, it can't call removeAllObjects second time round... Argh!
Can anyone help please?
Thanks!
removeAllObjects releases the objects in the array but has no effect on the array itself (except to make it empty).
Unless
one or more of the objects in the array had a retained reference to the array itself.
Okay so I was being an idiot!
I was doing:
[myArray removeAllObjects];
myArray = someOtherObject.someArray;
therefore resetting the pointer to some other object. I've now changed it to this:
[self.myArray removeAllObjects];
[self.myArray addObjectsFromArray:someOtherObject.someArray];
And it's now okay. I'm an idiot! Sorry all, thanks for your help!
removeAllObjects
does not release the array itself, as you rightly expect. If it is being released the problem is somewhere else. You can use the NSZombie's instruments to check where it is being released.
If you are autoreleasing your MutableArrays, they might be released when they are empty as they are after a call to removeAllObjects
. Use NSZombies to look where it is released exactly.
NSZombies can't tell you where the object is being released, so it shows you the first time you send a message to the deallocated object.
Work backward from that call to -removeAllObjects, and you'll find the bug.
[yourArray removeAllObjects];
it seems that the element of yourArray
will release aotuomatically
精彩评论