更新:-[NIndexPath row]:消息发送到已解除分配的实例0x895fe70
当我在设备上运行我的应用程序并进行分析时,它会说:
Objective-C消息已发送到地址为0xaa722d0的已释放对象(僵尸)。
它在以下位置显示错误:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int newRow = [indexPath row];
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row]: -1;
我是Objective C编程的新手,两天以来一直在努力解决以下问题。我最近使用Xcode 3.2.6创建了一个应用程序,但两天前升级到了Xcode 4,现在在下面的代码中面临内存释放问题。我研究了使用仪器并启用僵尸,了解问题发生在哪里,但无法解决问题,我请求一些指导。。下面是我在Xcode 4.2中运行的代码。
#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [list count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
CheckMarkCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CheckMarkCellIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
NSUInteger oldRow = [lastIndexPath row];
cell.textLabel.text = [list objectAtIndex:row];
cell.accessoryType = (row == oldRow && lastIndexPath != nil) ?
UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
#pragma mark -
#pragma mark Table Delegate Methods
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int newRow = [indexPath row];
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row]: -1;
//----Captures the value selected in the list and shown in parent view.
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
SchneiderBMSAppDelegate *appDelegate = (SchneiderBMSAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.priorityValue = cell.textLabel.text;
NSLog(@"Product selected is: %@",appDelegate.priorityValue);
if (newRow != oldRow)
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:
lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)viewDidUnload {
self.list = nil;
self.lastIndexPath = nil;
[super viewDidUnload];
}
- (void)dealloc {
[list release];
[lastIndexPath release];
[super dealloc];
}
您似乎在假设当lastIndexPath
发布时,它将变为零。事实并非如此。您要么需要保留对象(最好使用setter,而不是直接设置实例变量),要么不存储对它的引用。
我有一个应用程序,它使用了与我在iOS 4.3中完成的代码几乎相同的代码(记不起我引用了哪个教程),但没有发布到应用商店。当我最近恢复该应用程序并尝试在iOS 5中运行时,也遇到了完全相同的问题。我可以通过更改线路使其工作:
lastIndexPath=indexPath;
至
self.lastIndexPath=索引路径;
然后它在iOS 5中运行良好。我想它通过保留对象来完成与Chuck建议的相同的事情。但为什么iOS 5关心而不是iOS 4.3???这可能是iOS 5中的ARC造成的吗?