NSMutableArray - Can't always access it

Discussion in 'iOS Programming' started by Galaxy Bound, Feb 4, 2011.

  1. Galaxy Bound macrumors newbie

    Feb 4, 2011
    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?
  2. cnstoll macrumors 6502

    Aug 29, 2010
    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.

Share This Page