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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:01:03+00:00 2026-05-14T23:01:03+00:00

Frequent visitor but first post here on StackOverflow, I’m hoping that you guys might

  • 0

Frequent visitor but first post here on StackOverflow, I’m hoping that you guys might be able to help me out with this. I’m fairly new to Obj-C and XCode, and I’m faced with this really… weird… problem. Googling hasn’t turned up anything whatsoever. Basically, I get an EXC_BAD_ACCESS signal on a line that doesn’t do any dereferencing or anything like that that I can see. Wondering if you guys have any idea where to look for this. I’ve found a work around, but no idea why this works… The line the broken version barfs out on is the line:

LevelEntity *le = entity;

where I get my bad access signal.

Here goes:

THIS VERSION WORKS

  NSArray *contacts = [self.body getContacts];
  for (PhysicsContact *contact in contacts)
  {
   PhysicsBody *otherBody;
   if (contact.bodyA == self.body)
   {
    otherBody = contact.bodyB;
   }
   if (contact.bodyB == self.body)
   {
    otherBody = contact.bodyA;
   }

   id entity = [otherBody userData];
   if (entity != nil)
   {
    LevelEntity *le = entity;

    CGPoint point = [contact contactPointOnBody:otherBody];
   }
  }

THIS VERSION DOESNT WORK

  NSArray *contacts = [self.body getContacts];
  for (NSUInteger i = 0; i < [contacts count]; i++)
  {
   PhysicsContact *contact = [contacts objectAtIndex:i];
   PhysicsBody *otherBody;
   if (contact.bodyA == self.body)
   {
    otherBody = contact.bodyB;
   }
   if (contact.bodyB == self.body)
   {
    otherBody = contact.bodyA;
   }

   id entity = [otherBody userData];
   if (entity != nil)
   {
    LevelEntity *le = entity;

    CGPoint point = [contact contactPointOnBody:otherBody];
   }
  }

Here, the only difference between the two examples is the way I enumerate through my array. In the first version (which works) I use for (… in …), where as in the second I use for (…; …; …). As far as I can see, these should be the same.

This is seriously weirding me out. Anyone have any similar experience or idea whats going on here? Would be really great 🙂

Cheers,
Alex

  • 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-14T23:01:03+00:00Added an answer on May 14, 2026 at 11:01 pm

    First, if you have a crash, you have a backtrace. Always provide the backtrace with your question (it’ll be in the debugger and can be copy/pasted).

    As Vojito implied, the most common cause of crashes like these is related to the over-releasing of objects.

    In your case, for(;;) and for(... in ...) are not actually exactly the same. The latter is very likely causing the objects within the array to be retained for the duration of iteration or autoreleased upon retrieval (I say “very likely” because I didn’t test it — but it would explain the behavior).

    In your code, you are modifying your object graph during iteration with statements like otherBody = contact.bodyB. If any one of those statements happens to cause one of the items in the array being iterated to be released out from under the array, you would see a crash. Similarly, if the modification of the object graph causes either contact.bodyA or contact.bodyB to become a dangling reference, you would see a crash.

    All just an educated guess. Post the backtrace and, as Vojito suggested, run under the Allocation instrument in Instruments with zombie detection enabled.

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

Sidebar

Related Questions

No related questions found

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.