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 7518281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:43:45+00:00 2026-05-30T01:43:45+00:00

I (think I) know what you’re thinking… not another EXC_BAD_ACCESS question but I’m really

  • 0

I (think I) know what you’re thinking… not another EXC_BAD_ACCESS question but I’m really struggling and it’s driving me crazy. I’ve searched high and low all over the net and on here and the issue I’m facing seems have something to do with my memory management.

Problem:

Basically I have an NSMutableArray that keeps track of some cyber food. Since food can be added and taken away at the same time I have a separate array that checks for items to remove and holds the ones that don’t need to be taken out. The idea is to then clear the original array (_food) and copy back all the items saved in the temporary array (foodToKeep). Here’s the code

NSMutableArray *foodToKeep = [[NSMutableArray alloc] init];

for (Food *food in _food) {
    if(!food.removeable){
        [foodToKeep addObject:food];
        [food release];
    }
}

if(foodToKeep > 0){
  [_food removeAllObjects];

  for (Food *food in foodToKeep) {
      [_food addObject:food]; // EXC_BAD_ACCESS error here
      [food release];
      }
  }
}

[foodToKeep release];

I get the bad access error when add the food to the original array. After searching around it sounds like the _food array is being released somewhere or somehow ending up as nil. The only place I have a release for _food is in the dealloc method so I can’t understand why this is happening.

I’m fairly new to Objective-C (so go easy on me!) but as far as I can tell there are no leaks and no accidental releases that are causing this, I think what I need is an experts eye to see what is most likely a trivial mistake on my part 😛

Edit:
Food is defined and allocated here:

    @interface MainLayer
    {
        NSMutableArray *_food;
    }

In the class init method

    _food = [[NSMutableArray alloc] init];
  • 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-30T01:43:45+00:00Added an answer on May 30, 2026 at 1:43 am

    You are overreleasing here:

    for (Food *food in _food) {
        if(!food.removeable){
            [foodToKeep addObject:food];
            // [food release];  <--- REMOVE THIS
        }
    }
    

    And here

    for (Food *food in foodToKeep) {
          [_food addObject:food]; // EXC_BAD_ACCESS error here
          // [food release]; <--- REMOVE THIS
    }
    

    Remove the extra releases and you should be fine.

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

Sidebar

Related Questions

I think I know what a build is. But I am not sure. My
i think i know the answer to this question, but just want to make
I think I know the issue with this, but I do not know how
My apologies for the extremely basic question, I think I know the answer but
I'm not quite pro with encodings, but here's what I think I know (though
Apologies if this question has already been asked but I do not think I
I think I know why I am getting this error, but I'm not sure
This seems simple and I think I know the answer but I've searched and
My question is almost stupid because I think I know the answer but I
I think know how to do this in C# but I'm having syntax trouble

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.