gpt4 book ai didi

iphone - 重新加载 ViewController 后释放 placemark.postalCode

转载 作者:行者123 更新时间:2023-11-29 04:58:45 25 4
gpt4 key购买 nike

我有一个非常奇怪的问题(好吧,对我来说),我快疯了!我有一个 View Controller (X) 子类,其中我需要了解有关当前用户位置的邮政编码(和其他信息)。这样看来我可以回去了;当我重新加载 X 时,有时会出现此错误:* -[CFString isEqual:]:发送到已释放实例 0x6660660 的消息

问题是由 placemark.postalCode 生成的,我不明白 placemark.postalCode 何时何地被释放。

这是我的 View Controller 的代码:

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// reportsToUpdate = NO;
// locationValFound = NO;
NSLog(@"init city retain count %d", [city retainCount]);
}
return self;

}





- (void)dealloc
{
if(locationValFound)
NSLog(@"location found");
else NSLog(@"location not found");
EyesOnCityAppDelegate *appDelegate = (EyesOnCityAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.downloadQueue cancelAllOperations];
[request clearDelegatesAndCancel];
[request release];
[reportsTableView release];
mapView.delegate = nil;
[mapView release];
mapView = nil;
if (reverseGeocoder) {
NSLog(@"reverseGeo dealloc");
[reverseGeocoder cancel];
}
reverseGeocoder.delegate = nil;
NSLog(@"Reverse geo delegate nil");
[reverseGeocoder release];
reverseGeocoder = nil;
[reportsArray removeAllObjects];
[reportsArray release];
[reportImagesArray removeAllObjects];
[reportImagesArray release];

[waitMessageLabel release];
[waitAI release];
[genericVal release];
[viewType release];
[idType release];
[typeName release];
[cap release];
NSLog(@"dealloc retain count %d", [city retainCount]);
[city release];

[super dealloc];
}



- (void) reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
//NSLog(@"placemark %@", placemark);
NSLog(@"reverse geocoder placemark.postalcode %p", placemark.postalCode);
NSLog(@"fuori cap address %p", &cap);
if (placemark && ![placemark.postalCode isEqual:@""] && ![placemark.locality isEqual:@""] && !locationValFound) {
locationValFound = YES;
cap = placemark.postalCode;
NSLog(@"dentro cap address %p", &cap);
NSLog(@"reverseGeo prima city retain count %d", [city retainCount]);
city = placemark.locality;
NSLog(@"reverseGeo dopo city retain count %d", [city retainCount]);

NSLog(@"Cap: %@", cap);
NSLog(@"City: %@", city);

mapView.delegate = nil;
[mapView release];
mapView = nil;
[reverseGeocoder placemark];

[self downloadReports];
}
}



- (void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {

if (!locationValFound && ![reverseGeocoder isQuerying]) {
reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:userLocation.coordinate];

reverseGeocoder.delegate = self;
[reverseGeocoder start];
NSLog(@"reverseGeocoder retain %d", [reverseGeocoder retainCount]);
}
}

- (void) reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error {

}

这是我用来加载 View Controller 的代码:

ReportsViewController *reportsViewController = [[ReportsViewController alloc] initWithNibName:@"ReportsViewController" bundle:nil];

// [reportsViewController setReportsToUpdate:NO];
[reportsViewController setTypeName:[[typesArray objectAtIndex:indexPath.row] name]];
[reportsViewController setGenericVal:[[typesArray objectAtIndex:indexPath.row] generic]];
[reportsViewController setViewType:[[typesArray objectAtIndex:indexPath.row] type]];
[reportsViewController setIdType:[[typesArray objectAtIndex:indexPath.row] idType]];

//} UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedStringFromTable(@"BackBtnText", @"EyesOnCity_localized", @"") 样式:UIBarButtonItemStylePlain 目标:nil 操作:nil]; self.navigationItem.backBarButtonItem = backButton; [后退按钮释放];

[self.navigationController pushViewController:reportsViewController animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[reportsViewController release];

这些是输出中的一些行:

2011-09-14 19:01:21.097 eyesOnCity[1390:707] 反向地理编码器 placemark.postalcode 0x6660660

2011-09-14 19:01:21.099 eyesOnCity[1390:707] fuori 上限地址 0x663f29c

2011-09-14 19:01:21.101 eyesOnCity[1390:707] dentro 帽地址 0x663f29c

2011-09-14 19:01:21.104 eyesOnCity[1390:707] reverseGeo prima city 保留计数 0

2011-09-14 19:01:21.106 eyesOnCity[1390:707] reverseGeo dopo city 保留计数 3

2011-09-14 19:01:21.108 eyesOnCity[1390:707] 上限:12060

2011-09-14 19:01:21.110 eyesOnCity[1390:707] 城市:Niella Tanaro

2011-09-14 19:01:21.138 eyesOnCity[1390:707] 列表 ID:

2011-09-14 19:01:21.141 eyesOnCity[1390:707] lang: it_IT

2011-09-14 19:01:22.848 eyesOnCity[1390:707] 报告数量:7

2011-09-14 19:01:25.610 eyesOnCity[1390:707] 已找到位置

2011-09-14 19:01:25.614 eyesOnCity[1390:707] reverseGeo dealloc

2011-09-14 19:01:25.616 eyesOnCity[1390:707] 反向地理委托(delegate)为零

2011-09-14 19:01:25.622 eyesOnCity[1390:707] dealloc 保留计数 2

2011-09-14 19:01:25.626 eyesOnCity[1390:707] 报告单元格释放

2011-09-14 19:01:25.636 eyesOnCity[1390:707] 报告单元格释放

2011-09-14 19:01:25.648 eyesOnCity[1390:707] 报告单元格释放

2011-09-14 19:01:25.663 eyesOnCity[1390:707] 报告单元格释放

2011-09-14 19:01:28.450 eyesOnCity[1390:707] 初始化城市保留计数 0

2011-09-14 19:01:28.553 eyesOnCity[1390:707] 报告数量:0

2011-09-14 19:01:28.649 eyesOnCity[1390:707]reverseGeocoder 保留 2

2011-09-14 19:01:28.662 eyesOnCity[1390:707] 反向地理编码器 placemark.postalcode 0x6660660

2011-09-14 19:01:28.664 eyesOnCity[1390:707] fuori 上限地址 0x668d9bc

2011-09-14 19:01:28.666 eyesOnCity[1390:707] * -[CFString isEqual:]:发送到已释放实例 0x6660660 的消息

注意地址 0x6660660:重新加载 View Controller 后的相同地址

有什么想法吗?

<小时/>

我的问题是:如果在 didFindPlacemark 中地标对象是我从反向Geocoder 对象(每次加载 View Controller 时都是一个新对象)收到的对象,那么 placemark.postalCode 怎么可能被释放?我真的不明白我的代码有什么问题!而且内存管理看起来不错!

最佳答案

NSLog(@"reverseGeo prima city retain count %d", [city retainCount]);

请不要这样做。不要使用retainCount。不要依赖 retainCount。不要靠近retainCount

retainCount 并没有像你想象的那样做。这是苹果公司的相关文档(重点是我的):

This method is typically of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method

世界上最好的事情就是苹果更多地弃用它。它导致了很多很多问题 - 只需看看这些问题:

...这里有大量代表(在一种情况下,78k)的人说 retainCount 毫无值(value)。忽略那些说有用的人。事实并非如此。毫无疑问,有人会对此发表评论说,实际上,retainCount 完全可以使用。他们错了。苹果的文档说不要用它来调试。这里很多聪明人都说不要用它来调试。使用它是一个坏主意。

调试过度释放的内存问题(大概是这里发生的情况)的最佳方法是确保遵循 memory management rules 。这并不能特别帮助您解决实际问题,尽管浏览了您发布的代码后,我不确定问题是否立即显而易见(尽管它也不是 View Controller 的完整代码)。

Apple 提供了许多有用的工具来调试内存问题,包括构建和分析 (clang) 以及对象分配工具。使用它们比使用它们更好。

关于iphone - 重新加载 ViewController 后释放 placemark.postalCode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7428160/

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