I have written the following line of code:
NSMutableArray *array=[[NSMutableArray alloc]init];
This allocates some memory. My question is, how can we later release this memory, whether using removeAllObjects method or [array release]?
Do both methods have the same functionality?
When you add an object to the array, the object's retain count will increase by 1. When you remove that object from the array, retain count will decrease by 1 to balance it out. But if you release the array, all objects will automatically receive a release message. So you don't need to call removeAllObjects before releasing the array.
Technically, these two method are not same. If you call removeAllObjects, the array will become empty and all objects will receive a release message but the array itself is still not released. The array will be released when you call release on it.