【发布时间】:2011-11-02 11:06:45
【问题描述】:
当我在 UITableView 上向下滚动时,我的 iPhone 应用程序崩溃了。我将 NSZombieEnabled 设置为 YES,并发现我用来填充表格的 NSArray 正在以某种方式被释放。
#import "RootViewController.h"
@implementation RootViewController
@synthesize flashsets;
- (void)viewDidLoad
{
//Unrelated code removed from this post
NSString *listofsetsstring = [[NSString alloc]
initWithContentsOfFile:pathtosetsdata
encoding:NSUnicodeStringEncoding
error:&error];
flashsets = [listofsetsstring componentsSeparatedByString:@"\n"];
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [flashsets count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
NSLog(@"IndexPath.row = %i", indexPath.row);
cell.textLabel.text = [flashsets objectAtIndex:indexPath.row]; <<<< CRASH HERE!!
return cell;
}
@end
我在粗体线上收到message sent to deallocated instance 0x4ebae20。在我的 .h 中,我使用了 @property (nonatomic, retain) NSArray *flashsets;,我认为保留部分应该防止它被释放。
如何防止它这样做?
【问题讨论】:
标签: iphone memory-management nsarray