Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6658741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:56:56+00:00 2026-05-26T01:56:56+00:00

In my Brain model I have an NSMutableArray property called stockCars which holds 8

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:56:56+00:00Added an answer on May 26, 2026 at 1:56 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an NSMutableArray property defined in my Brain model. @property (retain, nonatomic) NSMutableArray
I have a method which calculates loan payments based on car price, interest rate
I have a ForeignKey which can be null in my model to model a
My brain is fried at the moment, so here's the scenario. I have a
I'm wrapping my brain around mobile sites using jquerymobile's framework. So far I have
I'm having a brain cramp... how do I initialize an array of objects properly
Until my brain get bleeding, still I can't solve this problem. I have to
I am using a 3D engine called Electro which is programmed using Lua. It's
I'm working on a Perl script which is called from a server side include
I know I have to be doing something just brain-dead stupid for this not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.