释放NSTimer的正确方法?
在我的 dealloc 方法中释放 NSTimer 的正确方法是什么?它是用以下代码创建的?
-(void)mainTimerLoop {
mainTimer = [NSTimer scheduledTimerWithTimeInterval:1/10
target:self
selector:@selector(gameLoop)
userInfo:nil
repeats:YES];
}
谢谢
What is the correct way to release a NSTimer in my dealloc method ? It was created with the following code ?
-(void)mainTimerLoop {
mainTimer = [NSTimer scheduledTimerWithTimeInterval:1/10
target:self
selector:@selector(gameLoop)
userInfo:nil
repeats:YES];
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
按照你这样做的方式,你永远不会点击
dealloc
。计时器保留其目标。在这种情况下,这意味着计时器已保留您。在其失效之前,它不会释放您。由于您创建了计时器,因此您还必须在dealloc
之前的某个时刻将其无效,因为计时器的保留将阻止您的对象被dealloc
处理。您有两个选择:
作为后者的一个例子:
注意时间间隔是
1.0/10.0
- 这也可以写成0.1
,但它不能写成1/10
,因为该除法将截断为0.0
。另请注意这如何打破保留周期:
The way you're doing it, you won't ever hit
dealloc
. A timer retains its target. In this case, that means the timer has retained you. It will not release you until it is invalidated. Since you created the timer, you must also invalidate it at some point prior todealloc
, because the timer's retain will prevent your object's beingdealloc
ed.You have two options:
As an example of the latter:
Notice how the time interval is
1.0/10.0
- this could also be written0.1
, but it cannot be written1/10
, as that division will truncate to0.0
.Also notice how this breaks the retain cycle:
有效的 NSTimer 由运行循环保留,如果它重复,它将永远保留或直到您使其无效为止。您不应该释放它,因为在您的示例代码中,您没有显式保留它。如果您使其无效,它将不再被运行循环保留,并且将被自动释放。
这对于重复计时器来说可能没问题,但对于一次性计时器来说很危险,因为在您访问它以查看它是否有效和/或尝试使其无效之前,它可能会被释放(这会导致不好的-访问应用程序崩溃)。因此,如果您计划以任何方式在创建计时器变量后查看它(包括检查它、使其无效和/或释放它),则最好将其显式保留在应用程序中的某个位置,然后无效后释放它并设置为 nil 就完成了。
如果将其声明为保留属性,则可以在一条语句中释放它并将其设置为 nil。然后你可以写:
A valid NSTimer is retained by the run loop, which, if it is repeating, will be forever or until you invalidate it. You shouldn't release it, since, in your example code, you did not explicitly retain it. If you invalidate it, it will no longer be retained by the run loop, and will be autoreleased.
This might be OK for a repeating timer, but is dangerous for a one-shot timer, since it might end being released before you ever access it to see if it's valid and/or try to invalidate it (which would lead to a bad-access app crash). Therefore if you plan on, in any way, looking at a timer variable after it's creation (including to check it, invalidate it and/or release it), it might be a good practice to explicitly retain it somewhere in your app, and then release it and set it to nil after it's invalid and you are done with it.
You can release it and set it to nil in one statement if you declare it as a retain property. Then you can write:
你在这里有一个关于 NSTimer 的非常好的答案 我如何使用 NSTimer? 他们在那里谈论关于停止重复的 NSTimer 行为
you have a really good answer about NSTimer here How do I use NSTimer? there they talk about stoping a repeating NSTimer doing
我认为这里最好的建议是 -
不要保留 NSTimer 实例,也不要释放它。
一旦它被调度到 NSRunloop(OP 示例中的当前运行循环)上,
NSTimer
就会被运行循环保留,直到失效或直到运行循环停止。应该做的,是在正确的时间使你的计时器无效 - 并且在你创建和调度它的同一线程上,
还要记住, NSTimer 保留其目标,并且不会让目标在它之前“死亡”。设计你的代码,这样你就没有一个保留周期,它会阻止你的对象(持有计时器)和计时器(持有你的对象)的释放。
I think the best advice here is -
Do not retain the
NSTimer
instance, and do not release it.As soon as it is scheduled on an
NSRunloop
(current runloop in the OP's example, anNSTimer
is retained by the runloop until being invalidated, or until the runloop stops.What you should be doing, is to invalidate your timer at the right time - and on the same thread where you created and scheduled it.
Keep in mind, also, that NSTimer retains its target, and won't let the target "die" before it dies itself. design your code so that you don't have a retain cycle that will prevent the releasing of both your object (holding the timer) and the timer (holding you object).
您不需要释放它,因为它会自动释放。任何由便捷方法创建的内容(即您自己不调用
alloc
)都是被调用函数负责内存管理,这通常意味着它将调用autorelease
它在返回之前创建的对象。我会使用
retain
关键字将计时器分配给一个属性,以确保它不会被释放给您。通常,如果自动释放的对象没有任何保留,则会在事件循环中释放它们。You don't need to release it because it will be autoreleased. Anything created by a convenience method (i.e. you don't call
alloc
yourself) is the responsibility of the called function to memory manage, which usually means that it will callautorelease
on the object it creates before it returns it.I would assign the timer to a property with the
retain
keyword though to make sure it doesn't get deallocated on you. Generally autoreleased objects are deallocated in the event loop if they don't have any retains.