自动失效但保留定时器失效
通过说
_requestTimer = [[NSTimer scheduledTimerWithTimeInterval:3.0
target:self
selector:@selector(updateSystems)
userInfo:nil
repeats:NO] retain];
我保留了一个 NSTimer,如果没有保留,它会自动失效(因为重复设置为 NO)。
我后来说,
[_requestTimer invalidate];
虽然这会使计时器失效,因为它会在没有保留的情况下自动失效,但我一直在想自动失效会发生什么? [_requestTimer invalidate];
是否释放我保留的引用以及将自动释放的引用?或者我有内存泄漏? 根据探查器,我发现了泄漏,但我不知道这是我的保留、自动保留还是探查器没有跟上(这不太可能)。
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 the 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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有 invalidate 不会释放你的计时器,如果你在创建后不需要处理它,你也不需要保留你的 NSTimer,例如,如果你不想阻止它触发,NSTimer 会保留在 NSRunLoop 中的某个地方它负责火环你的计时器。
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.
为什么不在 updateSystems 方法中将 _requestTimer 设置为 nil 并且不进行保留,然后通过测试 _requestTimer == nil 你知道你的计时器是否已触发(当然,发送到 nil 的任何消息都会被忽略,所以你赢了...... )
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....)