alloc init does not increase retain count - iphone

Basically if I do (not that I want to do this, just i'm trying to understand)
array = [[NSMutableArray alloc] init];
array = [[NSMutableArray alloc] init];
array = [[NSMutableArray alloc] init];
array should now be having a retain count of 3 right?
But somehow with me it doesn't

You have allocated three different NSMutableArrays, each one with a retain count of 1.
The first two of them are leaked.

Each time your creating a new array with its retain counter.
To increase retain counter, you would have to do something like:
array = [[NSMutableArray alloc] init];
[array retain];
[array retain];

nothing -/-> [array1 - count=1] (memory leak)
nothing -/-> [array2 - count=1] (memory leak)
array ---> [array3 - count=1]

No, it won't. Actually, the three arrays are not same. All would have different references.
array = [[NSMutableArray alloc] init];
[array retain];
This will increase the retain count.

Related

NSMutableArray alloc init vs NSMutableArray array

What is the differece between:
[[NSMutableArray alloc] init]
and
[NSMutableArray array]
Here in [NSMutableArray array] you don't have to release array it will be released automatically. & if you will write [NSMutableArray alloc] init] you will have to release array so [[NSMutableArray array] will be equivalent to [[[NSArray alloc] init] autorelease];
The first remains in memory until you release it, the second lasts until the end of the run loop iteration.
NSMutableArray no need to release memory and [NSMutableArray alloc] init] u must be release it.
when ARC does work, you have to release objects come from methods including init,alloc,new,copy and mutableCopy, like [NSMutableArray alloc] init]. If not, the objects will be registered to autoreleasepool, like [NSMutableArray array].

NSMutableArray memory Management when allocated directly

I know that when we add an object obj into a NSMutableArray, it takes the co_ownership of that object. So we can release the object obj just after adding that into the NSMutableArray. And when we release the NSMutableArray, it also calls the release of all the objects in it. So there is no memory leak and every thing is fine.
My doubt is if do like
NSMutableArray myArray = [[NMutableArray alloc] init];
[myArray addObject:[MyClass alloc] init]];
[myArray release];
Will this Cause any memory leak in our program?
Yes indeed, it will cause a memory leak.
The offensing line is (I have splitted it for clarity):
[myArray addObject:
[[MyClass alloc] init] // <- An instance is allocated with ownership
]; // <- The array retains the instance
At the end, the retain/release ownership are not balanced and causes the leak.
One solution is to make the allocation before the addition:
MyClass *obj = [MyClass alloc] init];
[[myArray addObject:obj];
[obj release];
Put autorelease for your MyClass. That should fix any memory leaks
NSMutableArray myArray = [[NMutableArray alloc] init];
[myArray addObject:[[[MyClass alloc] init] autorelease];
[myArray release];

Add dynamically created string to an array

I am making an iphone app in which I want to store the dynamically selected time into an array, but unable to implement the method to store the strings into an array. Following is the code which I am using but it is not giving the output.
- (void)storetimeintoanarray:(id)sender
{
NSDateFormatter *df3 = [[NSDateFormatter alloc] init];
[df3 setDateFormat:#"hh:mm:ss"];
timestr = [NSString stringWithFormat:#"%#",[df3 stringFromDate:objtimepicker.date]];
NSLog(#"time is:%#",timestr);
test = [[NSArray alloc]init];
[test arrayByAddingObject:timestr];
NSLog(#"array time:%#",test);
}
You have to declare array mutable object.
test = [[NSMutableArray alloc]init];
[test addObject:timestr];
You have to assign the result of the arrayByAddingObject: method to a new array like:
NSArray *newone = [test arrayByAddingObject:timestr];
After allocating you shouldn't allocate the array again. arrayByAddingObject returns a auto released new array. Also use a NSMutableArray when you want to add objects dynamically.
Change the code to
test = [[NSMutableArray alloc]init];
[test addObject:timestr];
You should be using NSMutableArray if you want to change it after creation.
NSMutableArray* arr = [[NSMutableArray alloc] init];
[arr addObject:timestr];
To create an array with a single object, you can use:
NSArray* arr = [NSArray arrayWithObject:timestr];
You should use NSMutableArray and its method addObject: instead of NSArray. [test arrayByAddingObject:timestr]; does nothing with your array test, its create new array

Select an array from an array of arrays to display in uitableview (iphone)

I have an array called addArray which I am adding array objects to:
NSMutableArray *addArray = [NSMutableArray array];
[addArray addObjectsFromArray: delegate.arrayobjectOne];
[addArray addObjectsFromArray: delegate.arrayobjectTwo];
// etc...
Now, if I only want to init one of these arrays to display in my table (preferably from another view controller but that's another question), how would I do this? And how would I access a specific property of each array object, e.g. arrayobjectOne.info?
Thanks for your time.
you can say
someObject = [addArray objectAtIndex: someIndex];
use this Example
NSMutableArray *objectsToAdd= [[NSMutableArray alloc] initWithObjects:#"one",#"two", nil];
NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:objectsToAdd,nil];
self.list = [[NSMutableArray alloc] init];
[self.list addObjectsFromArray:myArray];

Building an array of arrays

I am trying to get the following loop working to fill an array of arrays:
while (condition) {
[itemsArray fillFromDB];
if (! [checkArray containsObject:checkFlag]) {
// Add existing itemsArray to myArray
if (itemsArray.count) {
// add the itemsArray to myArray and create a new instance of itemsArray
[myArray addObject:itemsArray];
[itemsArray release];
NSMutableArray *itemsArray = [[NSMutableArray alloc] init];
}
[itemsArray addObject:myObject];
[checkArray addObject:checkFlag];
} else {
[itemsArray addObject:tmpEvent];
} }
However I try to shape this loop it falls over the release of itemsArray
when I use release (as above), the array does not re-initialise as a new instance with alloc. Whilst expecting emptyness, the next Object is added to the old array.
when I use removeAllObjects, my Array is emptied and so is the array attached to myArray.
Where am I going in the wrong direction?
You might place:
itemsArray = nil;
after the release message, to ensure that you're not pointing to an old instance.
EDIT
Looking at this again, you have:
NSMutableArray *itemsArray = [[NSMutableArray alloc] init];
This is scoped within the if statement.
So take out NSMutableArray and just use:
itemsArray = [[NSMutableArray alloc] init];
Don't write NSMutableArray *itemsArray = [[NSMutableArray alloc] init];--you're re-declaring the variable in the scope of the if statement, so outside the if statement, itemsArray will still refer to the old value (I'm not sure why the compiler isn't complaining). You can just say itemsArray = [[NSMutableArray alloc] init] instead.
You also might want to use autorelease, to simplify, as well.
The others have found the problem, but have created a new problem. The first time you create the mutable array, you need to use NSMutableArray *itemsArray = [[NSMutableArray alloc] init];. Then, after, you can release and use itemsArray = [[NSMutableArray alloc] init];. It is important that the first one (the one that creates the pointer) occurs only once, and the rest can occur as many times as desired.
EDIT:
You could write NSMutableArray *itemsArray; before the if statement, and then use itemsArray = [[NSMutableArray alloc] init]; in the if statement.