【发布时间】:2012-02-05 17:01:23
【问题描述】:
更新: -[NSIndexPath 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];
}
【问题讨论】:
-
请在 GDB 中使用 BT 命令更新您的问题,并提供错误描述。
-
无论问题是什么,简单地添加一个 [obj retain] 调用就可以解决它。
-
在 Xcode 中运行分析。什么/哪里有问题?在控制台中的错误信息是什么?
-
@RRB、CodaFi 和 CocoaFu - 非常感谢您的快速回复!我刚刚发布了我遇到的错误,这会有帮助吗?
-
@CodaFi:我尝试过,非常感谢!!它有帮助!它现在可以工作了:) 我替换了 lastIndexPath = indexPath; with lastIndexPath = [indexPath 保留];
标签: iphone objective-c ios