Is “removeAllObjects” supposed to release?

早过忘川 提交于 2019-12-07 02:19:27

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!