How to release Array objects in iphone - iphone

An array has contains multiple objects.If we call removeAllObjects method, Whether objects will
be released which in an array or we have release manually.

Sending an NSArray that message will cause it to release all its pointers. You don't need to release what you put in there yourself, unless you have your own pointers to those objects elsewhere.

You shouldn't need to remove all objects before you release. When an NSMutableArray is dealloc'd, it releases all of the objects in contains automatically.
More click here

You should do this to release your array. Better to release it this way rather than autoreleasing the array
[YourArray release];
YourArray = nil;
If you just want to get rid of the data and dont want to release the array then you can simply do:
YourArray = nil;
It is better to release array using [YourArray release] statement then declaring it as autorelease in declaration.
Hope this helps you.

Related

Info about re-alloc of an object

I have a question... If I have for example one NSMutableArray and if I use it... with for example 100 object... if I use this code:
mutablearray=[NSMutableArray alloc]init];
and add at the array others 100 objects... the first 100 object was deallocated (so the memory is free) or I must do [mutablearray release] to free memory?
thanks
if mutablearray is retained or using through copy property then surely you need to release it. And if you are not using mutablearray as property then you will need to look into the following:
Adding an object to an NSMutableArray increases it's retain count by one, giving it a retain count of 2 (1 for the alloc/init, and one for adding to the array).
so removeAllObject will take care of increased retaincount for the array and you need not to worry. Just call:
[mutablearray removeAllObjects];
You need or remove all objects from your array. In this case you don't need new array.
Or release array, at release array releases all it's objects.
You have to use [mutablearray release] to free memory because releasing the objects doesn't release the internal structures of the array.
Also, you shouldn't release objects in the array, instead remove them from the array by using any of the remove... commands of NSArray

(iphone) nsarray, nsmutablearray retain/release

this is so basic, but quite not sure how to release array object in object-c.
I know that adding an object to array retains the object.
and removing it from an array releases the object.
When I have an array(mutable or not),
does 'releasing the array itself' also removes objects inside the array(therefore sending release to each object)?
Or should I remove the object first and release the array to reverse the operation (which is retaining array and each object in it).
Thank you
Calling release on NSArray OR NSMutableArray instances will also release it's objects,

iphone - not leaking

What's the right way to do this?
I have an array that I will use on several methods. I will add objects to it, get values, replace values, etc.
Today I do this:
I declare it on .h, using something like
NSMutableArray *myArray;
as soon as the application starts, I declare it on .m doing something like
myArray = [[[NSArray alloc] init] retain];
If I don't add the retain the array will be released at some point and the application will crash. But allocating the array at the beginning of the application and left it "open" without releasing it will make instruments cry, pointing the finger at me, calling me a "leaker"...
How to solve that? Is this the correct way to do that? how do you guys do stuff like this?
thanks
alloc implicitly sets the retain count to 1. By sending the retain message you're incrementing the retain count to 2. In order for the object to be deallocated you would then need to release it twice. Failure to do so would result in a memory leak.
Ideally you should create the object in your init method using [[NSArray alloc] init] and then release it in your dealloc method like so:
- (void)dealloc {
[myArray release];
[super dealloc];
}
You might also find this article useful: http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html
One more thing: You declared myArray as an NSMutableArray but instantiated it as an NSArray. Perhaps that's causing the crash.
You should not retain the object you've just created. You already own it. If, as you say, "the array will be released at some point and the application will crash," that is the code you should change. Your code shouldn't be releasing an object that you still want to keep around.

Does removeObject release the object in an NSMutableArray of objects?

I was wondering when you remove an object using removeObject in an array if that removed object is handled properly. Would the object being removed be released?
The NSMutableArray will release it. If that's the last retain on it, it will be deallocated. From the documentation:
Like NSArray, instances of NSMutableArray maintain strong references to their contents. If you do not use garbage collection [Jed: the iPhone does not], when you add an object to an array, the object receives a retain message. When an object is removed from a mutable array, it receives a release message. If there are no further references to the object, this means that the object is deallocated. If your program keeps a reference to such an object, the reference will become invalid unless you send the object a retain message before it’s removed from the array.
See the NSMutableArray documentation. Their example, in fact, refers to removeObjectAtIndex::
id anObject = [[anArray objectAtIndex:0] retain];
[anArray removeObjectAtIndex:0];
[anObject someMessage];
Yes. Collections retain values they collect when the values are added to the collection, which means that the values are released when they're removed from the collection.
Yes, when the object is removed from the NSMutableArray, it is released. If its retain count is 0, it will be deallocated (or garbage collected, if you were instead running on OS X with GC enabled).
As everybody has said, the object of a NSMutableArray is released after it is removed from the array.
If you don´t want to release the object, retain it just before you call remove object method. In this case, you are responsible for it to release it later:
MyClass *objectToBeRemoved=[myArray objectAtIndex:indexPath.row];
[objectToBeRemoved retain];
[myArray removeObject:objectToBeRemoved];

Resetting NSMutableArray

What is the best a quickest way to reset an NSMutableArray?
-[NSMutableArray removeAllObjects] doesn't work for you?
removeAllObjects
removeAllObjects if assuming by 'reset', you mean you just want to empty the array.
If you are attempting to do what I think you are attempting to do, which is to keep an array empty but not release it, or at least to make it available next time it is needed then firstly you need to set a variable or a property within your class for this variable:
NSMutableArray *mutableArray;
Next add this code before the position at which you will need the empty array:
if (!mutableArray) {
mutableArray = [[NSMutableArray alloc] init];
}
Now you can safely call
[mutableArray removeAllObjects];
without fear that the array will become unavailable once empty.