【发布时间】: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