设置UILabel.text属性会导致线程中的内存泄漏

时间:2013-02-18 12:00:26

标签: ios nstimer

我很困惑!

我有一个计时器设置,在计时器内运行倒计时我用经过的时间更新UILabels一切都很好,但奇怪的是,当我通过仪器测试时,文本的设置会导致内存泄漏。

计时器代码是

NSTimer* timer = [NSTimer timerWithTimeInterval:1.0f
                                    target:self
                                    selector:@selector(myTimer:)
                                    userInfo:nil
                                    repeats:YES];
self.pnTimer = timer;
[[NSRunLoop mainRunLoop] addTimer:self.pnTimer forMode:NSRunLoopCommonModes];

我的计时器方法有以下内容......

- (void)myTimer:(NSTimer*)timer {
  ...
  if (self.secondsPassed <=9) {
     [self.secondsLabel setText:[NSString stringWithFormat:@"0%i",self.secondsPassed]];
  } else {
     [self.secondsLabel setText:[NSString stringWithFormat:@"%i",self.secondsPassed]];
  }
  ...

}

注释掉[self.secondsLabel ...行并在#Living列中查看Instruments,它大致保持原样,但允许代码运行并更新标签会导致#Living列不断增加,直到应用程序变得迟钝和反应迟钝(20分钟左右)

标签定义为

    @property (weak, nonatomic) IBOutlet UILabel *secondsLabel;

请注意,我也在使用iOS 6.1的iPhone 5设备上进行测试

任何想法我可能在这里做错了吗? 谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

好的,发现它是什么。 UI还具有光泽渐变Button UIButton + Glossy(http://www.mlsite.net/blog/?p=232)。我删除了按钮并更新标签不再导致内存泄漏。我假设每秒更新标签会导致完整的UI刷新,这也更新了自己有问题的按钮!

由于