gpt4 book ai didi

objective-c - NSIndexPath 的内存管理问题

转载 作者:行者123 更新时间:2023-11-28 23:00:41 26 4
gpt4 key购买 nike

所以目前我正在尝试保存 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];
}

获取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];
}

最佳答案

你正在分配一个 NSIndexPath,然后你用你的下一条语句覆盖它。更糟糕的是,您泄露了在第一条语句中分配的内存。这可能是静态分析器所关注的。导致崩溃的原因是您试图释放覆盖第一条语句中的对象的对象。由于它已经自动发布,这导致崩溃。

只需使用:

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

并删除发布声明。你应该很好。

关于objective-c - NSIndexPath 的内存管理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10067183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com