开发者

Is this memory safe?

开发者 https://www.devze.com 2023-01-16 20:57 出处:网络
[NSTimer scheduledTimerWithTimeInterval:0.033 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:0.033 target:self selector:@selector(gameLoop)
     userInfo:nil repeats:YES];

I don't do anything to dealloc or kill this timer after starting it. Is it safe? Or w开发者_Go百科ill it cause me to leak memory?


It shouldn't leak memory. You're not retaining the timer. The run loop will retain it (I think), but it'll release it when it no longer needs it.


The timer is retained by the run loop, so you don't need to retain it yourself.

However the timer will retain its target, so as long as it's repeating and you don't invalidate it, your target object won't be deallocated. You'll need to choose a good time to call invalidate on it which will cause the run loop to release it.

Note that you shouldn't also retain the timer yourself, at the risk of a retain cycle.

(I borked an answer to this very question yesterday and got schooled on it. Trying to atone.)

0

精彩评论

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