开发者

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(startTimer开发者_JAVA技巧s == 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.


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;


I already use Brians solution and still get EXC_BAD_ACCESS.

For me, a dispatch_after solved the problem:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
    if ([_timer isValid]) [_timer invalidate];
    _timer = nil;
    [self timer];
});

But it looks a bit dirty now, i know ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜