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

  • Home
  • SEARCH
  • 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 708721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:25:21+00:00 2026-05-14T04:25:21+00:00

I’m a beginner level programmer trying to make a game app for the iphone

  • 0

I’m a beginner level programmer trying to make a game app for the iphone and I’ve encountered a possible issue with the memory management (exc_bad_access) of my program so far. I’ve searched and read dozens of articles regarding memory management (including apple’s docs) but I still can’t figure out what exactly is wrong with my codes. So I would really appreciate it if someone can help clear up the mess I made for myself.

//in the .h file
@property(nonatomic,retain) NSMutableArray *fencePoleArray;
@property(nonatomic,retain) NSMutableArray *fencePoleImageArray;
@property(nonatomic,retain) NSMutableArray *fenceImageArray;

//in the .m file
- (void)viewDidLoad {
    [super viewDidLoad];
    self.gameState = gameStatePaused;

    fencePoleArray = [[NSMutableArray alloc] init];
    fencePoleImageArray = [[NSMutableArray alloc] init];
    fenceImageArray = [[NSMutableArray alloc] init];

    mainField = CGRectMake(10, 35, 310, 340);

    ..........

    [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
}

So basically, the player touches the screen to set up the fences/poles

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if(.......) {

    .......

    }
    else {
        UITouch *touch = [[event allTouches] anyObject];
        currentTapLoc = [touch locationInView:touch.view];

        NSLog(@"%i, %i", (int)currentTapLoc.x, (int)currentTapLoc.y);

        if(CGRectContainsPoint(mainField, currentTapLoc)) {
            if([self checkFence]) {
                onFencePole++;
                //this 3 set functions adds their respective objects into the 3 NSMutableArrays using addObject:
                [self setFencePole];
                [self setFenceImage];
                [self setFencePoleImage];

                .......

                }
            }
            else {
                .......
            }
        }
    }
}

The setFence function (setFenceImage and setFencePoleImage is similar to this)

-(void)setFencePole {
    Fence *fencePole;
    if (!elecFence) {
        fencePole = [[Fence alloc] initFence:onFencePole fenceType:1 fencePos:currentTapLoc];
    }
    else {
        fencePole = [[Fence alloc] initFence:onFencePole fenceType:2 fencePos:currentTapLoc];
    }
    [fencePoleArray addObject:fencePole];
    [fencePole release];

and whenever I press a button in the game, endOpenState is called to clear away all the extra images(fence/poles) on the screen and also to remove all existing objects in the 3 NSMutableArray. Point is to remove all the objects in the NSMutableArrays but keep the array itself so it can be reused later.

-(void)endOpenState {

    ........

    int xMax = [fencePoleArray count];
    int yMax = [fenceImageArray count];

    for (int x = 0; x < xMax; x++) {
        [[fencePoleImageArray objectAtIndex:x] removeFromSuperview];
    }

    for (int y = 0; y < yMax; y++) {
        [[fenceImageArray objectAtIndex:y] removeFromSuperview];
    }

    [fencePoleArray removeAllObjects];
    [fencePoleImageArray removeAllObjects];
    [fenceImageArray removeAllObjects];

    ........
}

The crash happens here at the checkFence function.

-(BOOL)checkFence {
    if (onFencePole == 0) {
        return YES;
    }
    else if (onFencePole >= 1 && onFencePole < currentMaxFencePole - 1) {
        CGPoint tempPoint1 = currentTapLoc;
        CGPoint tempPoint2 = [[fencePoleArray objectAtIndex:onFencePole-1] returnPos]; // the crash happens at this line
        if ([self checkDistance:tempPoint1 point2:tempPoint2]) {
            return YES;
        }
        else {
            return NO;
        }
    }
    else if (onFencePole == currentMaxFencePole - 1) {

    ......

    }
    else {
        return NO;
    }
}

So the problem here is, everything works fine until checkFence is called the 2nd time after endOpenState is called. So its like tap_screen -> tap_screen -> press_button_to_call_endOpenState -> tap screen -> tap_screen -> crash

What I’m thinking of is that fencePoleArray got messed up when I used [fencePoleArray removeAllObjects] because it doesn’t crash when I comment it out. It would really be great if someone can explain to me what went wrong. And thanks in advance.

  • 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-14T04:25:21+00:00Added an answer on May 14, 2026 at 4:25 am

    First, a couple of suggestions:

    if (!elecFence) {
        fencePole = [[Fence alloc] initFence:onFencePole
            fenceType:1 fencePos:currentTapLoc];
    }
    else {
        fencePole = [[Fence alloc] initFence:onFencePole
            fenceType:2 fencePos:currentTapLoc];
    }
    

    You’re making this too hard, how about this:

    const int fenceType = elecFence ? 2 : 1;
    Fence *fencePole = [[Fence alloc] initFence:onFencePole
            fenceType:fenceType fencePos:currentTapLoc];
    

    And this:

    int xMax = [fencePoleArray count];
    int yMax = [fenceImageArray count];
    
    for (int x = 0; x < xMax; x++) {
        [[fencePoleImageArray objectAtIndex:x] removeFromSuperview];
    }
    
    for (int y = 0; y < yMax; y++) {
        [[fenceImageArray objectAtIndex:y] removeFromSuperview];
    }
    

    Could be shortened using makeObjectsPerformSelector:

    const SEL remove = @selector(removeFromSuperview);
    [fencePoleImageArray makeObjectsPerformSelector:remove];
    [fenceImageArray makeObjectsPerformSelector:remove];
    

    This is shorter and safer, as the xMax bound in your code is computed from fencePoleArray and used to iterate over fencePoleImageArray. (Could be right, could be wrong.)

    Now to the objectAtIndex: call. If the array is still in memory and you tried to access an object beyond the array bounds, you would get an exception. So that I guess that either the array or some of the objects in it got released without you knowing it. You could try to NSLog the array and the object on given index and try to log their retainCount. If the logging line crashes, you have found the object that’s been released and can start looking for the cause.

    (And one more thing: You should split the game logic into a separate model class. This simplifies the code and makes it easier to reason about.)

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

Sidebar

Ask A Question

Stats

  • Questions 446k
  • Answers 446k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's resolution is 640x480, which from googling, seems to be… May 15, 2026 at 7:08 pm
  • Editorial Team
    Editorial Team added an answer In the markup: <asp:Image ID="image" runat="server" /> In the code-behind:… May 15, 2026 at 7:08 pm
  • Editorial Team
    Editorial Team added an answer "with" is, of course, a reserved word in Scala, so… May 15, 2026 at 7:08 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.