Exception thrown when constructing CGRect

I am making a game in which the player goes up to the exit and is transported to the next level. When I compile the project, I get the message: EXC_BAD_ACCESS (code = 2, address = 0x791)

I use this code to set up the exit:

_exit = [CCSprite spriteWithSpriteFrameName:@"exit.png"];
    CGPoint exitTileCoord = ccp(908, 980);
    CGPoint exitTilePos = [self positionForTileCoord:exitTileCoord];
    _exit.position = exitTilePos;
    [_batchNode addChild:_exit];

and I check whether the player has hit the exit with this code:

CGRect tankBoundingBox = CGRectMake(_tank.position.x - _tank.contentSize.width/2, _tank.position.y + _tank.contentSize.height, _tank.contentSize.width, _tank.contentSize.height);
CGRect exitBoundingBox = CGRectMake(_exit.position.x - _exit.contentSize.width/2, _exit.position.y + _exit.contentSize.height, _exit.contentSize.width, _exit.contentSize.height);
for (Tank *enemy in _enemyTanks) {
                if (CGRectIntersectsRect(sprite.boundingBox, enemy.boundingBox)) {

                    [childrenToRemove addObject:sprite];
                    enemy.hp--;
                    if (enemy.hp <= 0) {
                        [_enemyTanks removeObject:enemy];
                        [childrenToRemove addObject:enemy];
                    } else {
                    }
                }
            }

I get the error at the line where I set up the exitBoundingBox, but not the tankBoundingBox. Can someone tell me what I am doing wrong?

If it's any help, I am using Ray Wenderlich's tutorial to do this, and I copied the code exactly how he had it on his website (although I added in the BoundingBoxes).

Answers


Based on comments for the OP:

Not sure how ccp works, but it looks like one of the values you are using to construct the cgrect is dirty.


The problem in your code is that you are building a CGRect using the positions of some objects. Well , actually the problem is that one of those objects that you get the position of gets released. You should use NSZombie to detect which one and fix problem. Here's a reference that tells how to do that.

EDIT: I suspect the tank object to be the problem but I have to see more code on that. EDIT2: ccp is defined as CGRectMake. So there's no difference.


Need Your Help

Lua 5.2: undefined symbols when using luaL_dofile()

c++ c compiler-construction linker lua

I was trying to do some simple Lua 5.2 embedding using the following C++ code:

Qt copying extra files ino output/builddir

qt build qt-creator

The question is simple, I've even seen similar here, but still can't reach the desired result((