0

In my Brain model I have an NSMutableArray property called "stockCars" which holds 8 custom "Car" objects.

In my viewcontroller, I have an action set up that is triggered from 8 different UIButtons.

On the first click of one of the 8 buttons, I blank the Car object that is at the same index of the button that is pressed (using sender tag).

So after one press, I still have 8 Cars in stockCars array but one of them is a fresh alloc/inited (blank) one.

On the second press, I do the same thing and blank a second Car from stockCars.

on the third press a few things happen. First I blank the third Car from the array.

Then I do a bunch of other stuff...

Then I call a class method to replace any blank Cars in my stockCars array with a fresh random "Car" object which puts me back to 8 Cars in my array (no blanks).

Then, I want to start the process over... but for some reason it's giving an error on the 4th press. Here's my code... perhaps someone has an idea??

In my viewController button pressed action I'm doing this...

- (IBAction) addButtonPressed: (UIButton *)sender {

//My UIButtons have tags set 0-7
int senderTag = [sender tag];

[brain.stockIngredients replaceObjectAtIndex:senderTag withObject:[brain getBlankIngredient]];

if (brain.currentSelectedCarSlot == 2) {
...
[brain fillEmptyStock];
brain.currentSelectedCarSlot = 0;
}
else {
brain.currentSelectedCarSlot++;
}

Here's my fillEmptyStock method from Brain.m

- (void)fillEmptyStock 
{ 
NSMutableArray *tempArray = [self.stockCars mutableCopy];

for (Car * car in self.stockCars)
{
   if (car.name == nil){

        [tempArray replaceObjectAtIndex:[self.stockCars indexOfObject:car] withObject:[self getRandomCar]];
    }
}

[self.stockCars release];

self.stockCars = [NSMutableArray arrayWithArray:tempArray];

NSLog(@"sizeof stockCars %i", [self.stockCars count]);
[tempArray release];   
}

So yea, on the 4th press I get an error, but its one of those BAD EXCESS ones and it seems to be coming on the line [brain.stockIngredients replaceObjectAtIndex:senderTag withObject:[brain getBlankIngredient]];

Perhaps when I replace an object in an array it gets a new index and then that index is no longer valid or something?? I'm a beginner, thanks.

1
  • 2
    it looks like you don't have Static Analysis enabled, it will show you some memory management errors. Commented Oct 8, 2011 at 19:48

1 Answer 1

1

It seems that your problem lies at least partially in memory management issues. Assuming that stockCars is a retain property, you do not manually need to retain/release objects assigned to that property. Therefore, you do not need your line of code [self.stockCars release], since the assignment directly after it will release the old stockCars before assigning and retaining the new one.

EXC_BAD_ACCESS is triggered when your process attempts to access memory that it has not allocated, or has allocated and then freed. This is happening when the autorelease pool comes around to free the old stockCars, which has already been manually released by your inappropriate release call.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.