Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXC_BAD_ACCESS by resetting NSTimer

I want to reset two NSTimers with a new TimeInterval. It would be no problem if I know if they arent released before reseting them.

I can't work like this:

[timer invalidate];

if(startTimers == YES)
   timer = [NSTimer scheduledTimerWithTimerInterval:...]

because I don't know if the timer was invalidated before I invalidate it.

And if I invalidate a released timer (on invalidation a timer gets released) I get a EXC_BAD_ACCESS.

like image 374
mabstrei Avatar asked Dec 04 '22 23:12

mabstrei


1 Answers

When you release the timer, also set its variable to nil. Then [timer invalidate] will silently do nothing if timer is nil.

[timer invalidate];
timer = nil;
like image 155
Brian Avatar answered Dec 16 '22 20:12

Brian