开发者

Invalidation on auto-invalidation but retained timer

By saying

_requestTimer = [[NSTimer scheduledTimerWithTimeInterval:3.0 
                                                  target:self
                                                selector:@selector(updateSystems)
                                                userInfo:nil 
                                                  repeats:NO] retain];

I retain an NSTimer that would, without retain, auto-invalidate (since repeats is set to NO).

I later say

[_requestTimer invalidate];

While this invalidates 开发者_如何学Cthe timer, as it would do automatically without retain, I keep thinking what happens to the auto-invalidation? Does [_requestTimer invalidate]; release my retained reference and as well as the reference that would automatically release? Or do I have a memory leak? According to the profiler, I get a leak, but I don't know if it's my retain, the auto-retain or the profiler not catching up (which would be highly unlikely).


No invalidate does not release you timer, you also do not need to retain your NSTimer if you do not need to deal with it after creation, for example if you do not want to stop it from firing, the NSTimer is retained somewhere within the NSRunLoop which is responsable for fire ring your timer.


Why not just set _requestTimer to nil in your updateSystems method and not do the retain, then by testing _requestTimer == nil you know if your timer has fired (and of course any messages sent to nil are ignored anyway so you win....)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜