jowie jowie - 5 months ago 9
Objective-C Question

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 call 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!

Answer

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!

Comments