Objective c nsindepath的内存管理问题

Objective c nsindepath的内存管理问题,objective-c,ios,xcode,memory-management,Objective C,Ios,Xcode,Memory Management,因此,目前我正试图保存一个indexPath并访问它,但我一直收到EXC_BAD_access错误,当我在xcode中使用分析工具时,它说在初始化期间存储到indexPath的值永远不会被读取。有谁能帮忙告诉我这里出了什么问题吗 要设置indexPath的方法: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSURL *requestURL = [[N

因此,目前我正试图保存一个indexPath并访问它,但我一直收到EXC_BAD_access错误,当我在xcode中使用分析工具时,它说在初始化期间存储到indexPath的值永远不会被读取。有谁能帮忙告诉我这里出了什么问题吗

要设置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];
}
-(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];
}
访问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];
}
-(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];
}

首先,分配一个新对象,并将其存储到
indepath
中。然后,使用先前存储在
didSelectRowAtIndexPath
中的索引路径覆盖新分配的索引路径。因此,新分配的索引路径将丢失,您将得到一个错误


更重要的是,您试图释放存储在
didSelectRowAtIndexPath
中的这个对象,而一开始没有“拥有”它,因此您的应用程序崩溃。

您正在分配一个nsindepath,然后您正在用下一条语句对其进行写入。更糟糕的是,您泄漏了在第一条语句中分配的内存。这可能就是静态分析器所关注的。导致崩溃的原因是您试图释放从第一条语句中重写对象的对象。由于它已经自动释放,这将导致崩溃

只需使用:

NSIndexPath *indexPath = [request.userInfo objectForKey:@"indexPath"];

然后扔掉发布声明。您应该很好。

您已经解决了indexPath的问题,但由于某些原因它仍然崩溃。我想这与用户信息词典有关,但是你有没有想过为什么我仍然会获得EXC\u BAD\u访问权限?我根据您的建议更改了indexPath代码,并删除了[indexPath版本]。。@derek.lo您的崩溃仍然发生在同一个地方吗?您不应该仅仅因为访问
[request.userInfo objectForKey:@“indepath”]而崩溃(虽然它可能会返回nil)。我已经用崩溃发生的完整方法编辑了我的帖子。这可能是我将对象从一个视图转移到另一个视图的方式吗???@derek.lo您不应该释放您的
请求,因为它是自动释放的。但是还有一件事我想知道:如果
请求
是自动删除的,并且您将要使用它执行异步任务,那么我认为您应该实际保留它,直到您收到任务已完成的通知。您确定了它。非常感谢。但是我想跟进我们是否应该保留这个请求直到它被发布。只要您确定要在RequestDidSuccess或requestDidFail中释放[request retain],调用[request retain]是否会有伤害?再次感谢你的帮助