NSTimer 内存泄漏?

发布于 2024-12-10 13:55:30 字数 571 浏览 0 评论 0原文

以下内容是否会导致内存泄漏,或者是否可以以某种方式进行更好的更改?随着 countDownTimer = nil 被删除

-(void)viewDidLoad{    
  countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(loadNewTime:) userInfo:nil repeats:YES];
}

-(void)pauseTimer{            
   NSLog(@"Fired");
   [countDownTimer invalidate];
   //countDownTimer = nil <------ Causes crash when run
}

-(void)resumeTimer{
   countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(loadNewTime:) userInfo:nil repeats:YES];
}

Will the following cause a memory leak or could it be changed to be better somehow? With the countDownTimer = nil being removed

-(void)viewDidLoad{    
  countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(loadNewTime:) userInfo:nil repeats:YES];
}

-(void)pauseTimer{            
   NSLog(@"Fired");
   [countDownTimer invalidate];
   //countDownTimer = nil <------ Causes crash when run
}

-(void)resumeTimer{
   countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(loadNewTime:) userInfo:nil repeats:YES];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪眸﹌ 2024-12-17 13:55:30

可以使用 scheduleTimer 方法的 block 变体来避免循环引用。

var timer = Timer()

func startTimer() {
  timer = Timer.scheduleTimer(withTimerInterval: 1.0, repeats: true, block: { [weak self] (timer) in
     self?.performUpdate(timer: timer)
  })
}

func stopTimer() {
   timer.invalidate()
}

只需确保将 self 捕获为 weak 变量即可。

One can use block variant of scheduleTimermethod to avoid retain cycles.

var timer = Timer()

func startTimer() {
  timer = Timer.scheduleTimer(withTimerInterval: 1.0, repeats: true, block: { [weak self] (timer) in
     self?.performUpdate(timer: timer)
  })
}

func stopTimer() {
   timer.invalidate()
}

Just make sure to capture self as a weak variable.

只是在用心讲痛 2024-12-17 13:55:30

当 NSTimer 过期或失效时,它会自动减少其保留计数。没有必要将其设置为 nil,除非您已将其分配给保留的 ivar。 (在您的情况下,这意味着您还需要使用 self.countDownTimer 分配计时器

NSTimer automatically decrements its retain count when it expires or is invalidated. There is no need to set it equal to nil unless you have assigned it to an ivar that retains. (which in your case means you would have also needed to assign the timer using self.countDownTimer

毁梦 2024-12-17 13:55:30

我假设 countDownTimer 是一个 ivar? NSTimer 在存活时保留其目标,因此听起来您对 [countDownTimer invalidate] 的调用正在释放对 self 的最后一个引用并导致 self-pauseTimer 方法中间释放。因此,任何对 self 的引用,甚至是隐式引用(例如访问 ivars)都会崩溃,或者至少会出现意外行为。

谁在调用 -pauseTimer?也许他们应该对您的对象有强烈的参考。

I assume countDownTimer is an ivar? NSTimers retain their targets while they're alive, so it sounds like your call to [countDownTimer invalidate] is releasing the last reference to self and causing self to deallocate in the middle of your -pauseTimer method. As such, any reference to self, even implicit references (e.g. accessing ivars) is going to crash, or at least behave unexpectedly.

Who is calling -pauseTimer? Perhaps they should have a strong reference to your object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文