Update: -[NSIndexPath row]: message sent to deallocated instance 0x895fe70
When I run my app on device and Analyze it says:
An Objective-C message was sent to a deallocated object (zombie) at address: 0xaa722d0.
It’s showing an error at:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int newRow = [indexPath row];
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row]: -1;
I am very new to Objective C programming and have been struggling with the below issue since two days, I recently created an app using Xcode 3.2.6 but upgraded to Xcode 4 two days back and now facing memory deallocation issues in my below code. I researched on using instruments and enabled zombie and understood where the problem is occurring but just not able to resolve it,
I request some guidance please.. below is my code being run in 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];
}
You seem to be assuming that when
lastIndexPathis released, it will become nil. That isn’t so. You either need to retain the object (preferably by using a setter rather than setting the instance variable directly) or not store a reference to it.