1

I get NSRangeException error when I try to removeObjectAt:0 . I don't know how that is even possible.

-(void)removePoints:(ccTime *)tm
{

    NSLog(@"naughty %@",_naughtytoucharray);
    if ([_naughtytoucharray count]>0)
    {
        [_naughtytoucharray removeObjectAtIndex:0];
    }
}

shows on log:

[4195:c07] naughty ( "{124, 98}", "{123, 98}", "{135, 97}", "{124, 98}", "{148, 94}", "{135, 97}", "{157, 93}", "{148, 94}", "{162, 92}", "{157, 93}", "{164, 92}", "{162, 92}" )

then I get the following error: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 11 beyond bounds [0 .. 10]'

When I set a breakpoint I see all objects are 0x0

enter image description here

NSMutableArray is allocated and initialized in init and filled in ccTouchesMoved

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  {


    if (isSelected) {

        UITouch *touch = [ touches anyObject];
        CGPoint new_location = [touch locationInView: [touch view]];
        new_location = [[CCDirector sharedDirector] convertToGL:new_location];


        CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
        oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
        oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];

        // add my touches to the naughty touch array
        [_naughtytoucharray addObject:NSStringFromCGPoint(new_location)];
        [_naughtytoucharray addObject:NSStringFromCGPoint(oldTouchLocation)];

    }
}

Any idea why I cannot remove the first item of the array?

2
  • See if accessing your mutable array with its property self.naughtytoucharray makes a diffrent. Commented Nov 26, 2012 at 21:23
  • I'm guessing you screwed up something pretty badly in storage management, and the array is getting corrupted. Commented Nov 26, 2012 at 21:31

1 Answer 1

5

That's not where your error is coming from. The method it's complaining about is objectAtIndex:, not removeObjectAtIndex: and the index causing the error is eleven, not zero.

Look for a place in your code where you are assuming that there are at least 12 objects in the array while reading from it. There are only eleven and their indexes are 0-to-10.

Sign up to request clarification or add additional context in comments.

1 Comment

ok I get what you mean, I read array every frame in -(void)draw

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.