So currently I am attempting to save an indexPath and access it, yet I keep receiving the EXC_BAD_ACCESS error, and when I use the Analyze tool in xcode, it says that the value stored to my indexPath during its initialization is never read. Can anybody help and tell me what’s going wrong here?
Method to set indexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSURL *requestURL = [[NSURL alloc] initWithString:@"URL"];
//The request
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:requestURL];
request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:indexPath,@"indexPath", nil];
[request setDelegate:self];
[request startAsynchronous];
[requestURL release];
[request release];
}
Method to access indexPath:
-(void)requestFinished:(ASIHTTPRequest *)request{
UIImage *foodImage = [[UIImage alloc] initWithData:[request responseData]];
NSIndexPath *indexPath = [request.userInfo objectForKey:@"indexPath"];
FoodDescription *detailViewController = [[FoodDescription alloc] initWithNibName:@"FoodDescription" bundle:nil];
// pass the food
detailViewController.aFood = [[NSMutableDictionary alloc] initWithDictionary:[_foodArray objectAtIndex:indexPath.row]];
detailViewController.foodPicture = foodImage;
detailViewController.restaurantName = _restaurantName;
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
You’re allocating a NSIndexPath, and then you’re writing over it with your next statement. What is worse, you’ve leaked the memory allocated in the first statement. This is probably what the static analyzer was picking up on. What causes your crash is that you’re attempting to release the object that overwrote the object from the first statement. Since it’s already autoreleased, this will lead to a crash.
Just use:
and get rid of the release statement. You should be good.