原文:
[timer release] only needs to be called if you “own” the timer. From Apple’s documentation:
Because the run loop maintains the timer, from the perspective of memory management there’s typically no need to keep a reference to a timer once you’ve scheduled it. Since the timer is passed as an argument when you specify its method as a selector, you can invalidate a repeating timer when appropriate within that method. In many situations, however, you also want the option of invalidating the timer—perhaps even before it starts. In this case, you do need to keep a reference to the timer, so that you can send it an invalidate message whenever is appropriate. If you create an unscheduled timer (see “Unscheduled Timers”), then you must maintain a strong reference to the timer (in a reference-counted environment, you retain it) so that it is not deallocated before you use it.
What does this mean?
If you alloc and init a timer, you must also release it, like so:
NSTimer * timer = [[NSTimer alloc] initWith…];
NSRunLoop * runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
[timer release];
……
[timer invalidate];
timer = nil;
Once the timer has been added to the run loop, there is no reason to keep a reference to it anymore, since the run loops owns it. In this case, as shown, you would release the timer as soon as you add it to the run loop, and then simplyinvalidate it when you are finished. The final line (setting timer to nil) is for safety. The call to invalidate will result in the timer being released (by the run loop), so it is unsafe to keep a reference that points to it. Setting the local reference to nil keeps things kosher.
If, however, you create a timer using one of the convenience methods like so:
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval ...];
You do not need to call [timer release] at all!
The convenience method adds the timer to the run loop, which then owns it, so you do not need to perform any memory management on the returned timer object. You would simplyinvalidate the timer when you no longer want to use it:
[timer invalidate];timer = nil;
Or, if the timer was not set to repeat, you would do absolutely nothing at all, since it would be released after its first invocation.
NSTimer * timer = [[NSTimer alloc] initWith…];
NSRunLoop * runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
[timer release];
……
[timer invalidate];
timer = nil;
一旦timer被加入了Runloop,我们就没有任何原因来保持一个引用,因为Runloop会保持它。在这个展示中,正如我们我展示的,你应该release 掉它,当你把它添加进Runloop的时候,并且像示例中的那样在timer结束的时候 invalidate 。最后一行代码(timer = nil)是为了安全。invalidate 的调用会使得timer被release(由Runloop控制),所以保持一个指向timer的引用是不安全。将本地的引用设置为空是符合规则的。
无论如何,如果你创建一个timer按照下面的这种形式:
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval ...];
你根本不需要 调用 [timer release]!
这个便捷的方法,把timer加入到了Runloop,Runloop会保持timer的引用计数,所以你不需要在返回的timer对象上实现任何的
内存管理。你应该想示例的那样invalidate timer,如下:
[timer invalidate];timer = nil;
或者,如果timer没有设置重复(repeat),你就安心的不需要做任何的事情,因为他会在第一次调用完成后release timer。