views:

89

answers:

2

Hi,

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.

A: 

If you want to use properties, you should use self.propertyName = ... instead of propertyName = ....

Hope this will help.

kovpas
Where exactly? In `viewDidLoad` that would actually cause a leak.
zoul
A: 

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.)

zoul
Thanks for the suggestions, but it still didn't help me with the memory problem. I did a test using the NSZombieEnabled variable and I got a error message which says:*** -[Fence returnPos]: message sent to deallocated instance 0x3933000I'm assuming that my fencePoleArray really did get over-released but I can't see where is it that the over-releasing occurred.
That’s great, you’ve almost got it. Now all you have to do is set up a breakpoint in the `release` method of `Fence` and see where you release your instances. It should be a matter of minutes to spot the offender (look in the stack trace).
zoul
How can I do that? I've checked through my codes but I never did explicitly put any code to release my fencePoleArray. Closest match goes to [fencePoleArray removeAllObjects] which only releases the objects inside the array but retains the array itself (this is my assumption that the array is retained even when all objects inside is removed so please correct me if i'm wrong).
The array itself is fine. The `NSZombieEnabled` test tells you that it’s the `Fence` that gets over-released. Override the release method in `Fence` (simply calling `[super release]`), set up a breakpoint there and see where the fences get released.
zoul
And yes, do you realize that `removeFromSuperview` releases the receiver?
zoul
I'm not exactly sure of what you meant by "releases the receiver" but my setFenceImage function recreates a new image to add to subview whenever its called.
ok I managed to solved the problem(seemingly)! It seems that I did not add fencePole = nil; after i release it so now it works after i added in that line of code to all my release statements. at least it didn't crash on me.