【问题标题】:ARC Memory Leak in cellForRowAtIndexPath when accessing [EKEvent notes]访问 [EKEvent notes] 时 cellForRowAtIndexPath 中的 ARC 内存泄漏
【发布时间】:2013-01-17 10:23:49
【问题描述】:

我将 ARC 与 xCode 4.6 结合使用,但在尝试访问 EKEvent.notes 属性以分配给 cellForRowAtIndexPath 中的字符串时出现内存泄漏。

有趣的是,泄漏永远不会出现,直到我在任何单元格中切换“付费/未付费”按钮,它不会调用 cellForRowAtIndexPath,尽管这两种方法都需要访问包含我的 EKEvents 数组的单例.

根据 Instruments 的说法,泄漏是因为这条线而发生的:

eventNotes = [NSString stringWithFormat:@"%@", [currentEvent notes]];

eventNotes 是我在 .h 文件中声明的 ivar

我尝试了以下使用ivar 并动态创建新实例变量,但似乎没有任何效果:

NSString *eventNotes = [NSString stringWithFormat:@"%@", [currentEvent notes]];
eventNotes = [currentEvent notes];
eventNotes = currentEvent.notes;
NSString *eventNotes = [currentEvent notes];
NSString *eventNotes = currentEvent.notes;
NSString *eventNotes = [NSString stringWithString:[currentEvent notes]];
NSString *eventNotes = [NSString stringWithString:currentEvent.notes];
eventNotes = [NSString stringWithString:currentEvent.notes];

列表还在继续,这是我的 cellForRowAtIndexPathCode,发生泄漏的地方。 我对内存管理完全陌生,所以请!是描述性的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

     //Custom Cell...
     UTATableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
     cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableviewCell.png"]];
     cell.textLabel.backgroundColor = [UIColor clearColor];
     cell.detailTextLabel.backgroundColor = [UIColor clearColor];

     //reset cell view attributes
     cell.paidButton.tag = 1;
     [cell.paidButton setAlpha:1.0f];
     [cell.paidButton setSelected:NO];
     [cell.paidButton setEnabled:YES];

     cell.title.tag = 2;
     cell.title.text = NULL;

     cell.subtitle.tag = 3;
     cell.subtitle.text = NULL;

     [cell.paidButton addTarget:self action:@selector(togglePaid:) forControlEvents:UIControlEventTouchUpInside];

     if ([_yearSwitcher selectedSegmentIndex] == 0) {

          //lastYearsUTAEvents is an MSMutable array and a property of sharedEventData, which is a singleton class...             
          NSMutableArray *currentArray = [sharedEventData.lastYearsUTAEvents objectAtIndex:indexPath.section];
          EKEvent *currentEvent = [currentArray objectAtIndex:indexPath.row];
          NSDate *date = [currentEvent startDate];
          NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
          [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
          [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
          NSString *dateString = [dateFormatter stringFromDate:date];
          cell.title.text = dateString;

-leak---> eventNotes = [NSString stringWithFormat:@"%@", [currentEvent notes]];

          if ([eventNotes rangeOfString:@" - Paid"].location != NSNotFound) {
               cell.subtitle.text = [eventNotes stringByReplacingOccurrencesOfString:@" - Paid" withString:@""];
               [cell.paidButton setSelected:YES];

          } else {        
               cell.subtitle.text = eventNotes;
               [cell.paidButton setSelected:NO];
          }

      } else {

          // same as above but different array....
      }
  return cell;

}

【问题讨论】:

    标签: ios memory-leaks nsstring ekevent


    【解决方案1】:

    解决方案是在从 EKEvents 数组访问 EKEvent 对象时强制转换 (EKEvent *)

    我从日历中提取事件并将它们存储在一个名为 lastYearsUTAEventsNSMutableArray 中(这是一个单例类的属性)。

    当访问数组中的事件时,我不得不使用强制转换来防止由于某种原因导致内存泄漏。所以而不是:

     NSMutableArray *currentArray = [sharedEventData.lastYearsUTAEvents objectAtIndex:indexPath.section];
     EKEvent *currentEvent = [currentArray objectAtIndex:indexPath.row];
    

    我将对象转换为 (EKEvent *),同时将其从数组中取出,现在没有任何泄漏。 (我还把前面2行代码优化成了1行):

    EKEvent *currentEvent = (EKEvent *)[[sharedEventData.thisYearsUTAEvents objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    

    起初,我什至根本无法更改.notes 属性,因为在documentation 中,它是NSString,而不是NSMutableString,所以我假设当你保存一个EKEvent(或者,在我的情况下,编辑一个)它会保存现有的事件,因为没有用于保存事件的替换方法。仅保存和删除。

    除此之外,.notes 甚至不是EKEvent 的属性。这是EKCalendarItem 的属性。

    希望这对将来的某人有所帮助...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      • 2012-03-04
      • 2012-09-14
      • 2014-06-15
      • 2012-10-05
      • 1970-01-01
      相关资源
      最近更新 更多