【发布时间】:2011-08-16 21:16:41
【问题描述】:
我在 UIViewController 中有一个 MapView。当我尝试在 viewDidUnload 之后加载视图时,它会崩溃并显示以下消息:
-[CALayer release]:消息发送到deallocated instance 0x29aea0
我认为我在 viewDidUnload 中做了所有我应该做的事情:
- (void)viewDidUnload {
[super viewDidUnload];
locationManager.delegate = nil;
[locationManager release];
locationManager = nil;
mapView.delegate = nil;
[mapView release];
mapView = nil;
}
我的 MapView 位于 xib 文件中配置的 UIView 中。我的 VC 永远不会被释放。
我已经在谷歌上搜索了一段时间,但找不到答案。
编辑
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
[label setFont:[UIFont fontWithName:@"Sansation" size:28]];
[label setShadowOffset:CGSizeMake(0, 1)];
[label setShadowColor:[UIColor grayColor]];
self.navigationItem.titleView = labelView;
[label release];
[labelView release];
UIBarButtonItem *checkInButton = [[UIBarButtonItem alloc] initWithTitle:@"Checka In" style:UIBarButtonItemStylePlain target:self action:@selector(checkIn)];
[[self navigationItem] setRightBarButtonItem:checkInButton];
[checkInButton release];
UIBarButtonItem *clueListButton = [[UIBarButtonItem alloc] initWithTitle:@"Ledtrådar" style:UIBarButtonItemStylePlain target:self action:@selector(cluesDown)];
[[self navigationItem] setLeftBarButtonItem:clueListButton];
[clueListButton release];
UINavigationBar *bar = [self.navigationController navigationBar];
[bar setTintColor:[UIColor colorWithRed:0.06 green:0.58 blue:0.88 alpha:1]];
}
“labelView”和“label”是 IBOutlets。
【问题讨论】:
-
locationManager和mapView真的真的保留了吗?我想它们只是作为子视图添加而没有保留,然后
[super viewDidUnload];可能会释放它们,而您的释放会导致崩溃。 -
locationManager = [[CLLocationManager alloc] init];和@property(非原子,保留)IBOutlet MKMapView *mapView;。这是正确的方法吗?如果 [super viewDidUnload] 正在释放对象,我应该在 viewDidUnload 中获得 exc_bad_access 吗?错误出现在 viewDidAppear 之后,但在您真正看到 mapView(只是网格)之前。
-
在访问已释放的对象时不一定会遇到错误的访问异常。如果新对象现在位于该内存位置,您还可以获得 message sent to deallocated instance 错误或 unrecognized selector。但是你现在说消息是在 viewDidAppear 之后出现的,所以你应该向我们展示这个方法。
viewDidUnload方法在视图控制器释放其视图(清理)时被调用。 -
viewDidAppear 方法中没有写任何内容。我只是在那里有一个断点,看看它是否被调用。我将编辑我的问题并添加 viewDidLoad 方法。
标签: iphone objective-c release android-mapview calayer