View Full Version : NSMutableArray - Can't always access it

Galaxy Bound
Feb 4, 2011, 09:47 PM
When trying to read values from my nsmutablearray, it errors with EXC_BAD_ACCESS

I believe I have loaded the array correctly in my init statement:
arraystore= [NSMutableArray arrayWithCapacity:10];
self.myarray = arraystore;
[arraystore release];

During init i call this in a separate void:
[self.myarray addObject:[NSValue valueWithPointer:Shape]];
NSLog(@"added %@ to Array",[self.myarray objectAtIndex:0]);
The above code works fine.
But then when I want to access it in a seperate void after it just crashes when i try to read from it, or count etc...

for(int i =0;i<[self.myarray count];i++) //crashes here
NSLog(@"dealing with - %@",[self.myarray objectAtIndex:i]);
//Or simply this :
NSLog(@"dealing with - %@",[self.myarray objectAtIndex:0]);

What is wrong with this code that is causing it to crash?

Feb 4, 2011, 10:37 PM
You don't need to release arraystore in your init method. The object that is returned by arrayWithCapacity has already been autoreleased, so by releasing it yourself you're actually causing it to be dealloc'd before you have a chance to read it. That's what's causing the bad access.