iPhone NSTimer OpenGL problem
- by Toby Wilson
I've got a problem that only seems to occur on the device, not in the simulator.
My app's animation is started and stopped using these methods:
NSTimer* animationTimer;
-(void)startAnimation
{
if(animationTimer = nil)
animationTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f/60.0f target:self selector:@selector(drawView) userInfo:nil repeats:YES];
}
-(void)stopAnimation
{
[animationTimer invalidate];
animationTimer = nil;
}
In the simulator this works fine and drawView starts being called at 60fps.
On the device (testing on iPod Touch), the scheduleTimerWithTimeInterval method doesn't seem to work. Furthermore, [animationTimer invalidate] causes EXC_BAD_ACCESS.
I've spotted an obvious but minor flaw; adding if(animationTimer != nil) to the stopAnimation method will prevent the crash, but doesn't solve the problem of the animation timer not being properly initialised.
Edit: The above doesn't prevent a crash. animationTimer != nil yet calling invalidate causes EXC_BAD_ACCESS.
Should also add, this problem doesn't occur all the time on the device. Maybe 40% of the time.