gpt4 book ai didi

iPhone - 类的实例...正在被释放,而键值观察仍在其中注册

转载 作者:行者123 更新时间:2023-12-03 18:38:31 24 4
gpt4 key购买 nike

由于 NavigationController,我有一个被插入的 ViewController(带有 MKMapView)。所以我有一个带有“后退”按钮的导航栏。单击该后退按钮,我收到错误:

2010-01-11 18:05:35.273 TestApp[147:207] An instance 0x1758f0 of class MKUserLocation is being deallocated while key value observers are still registered with it. Observation info is being leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info: ( Context: 0x0, Property: 0x17d600> ) Program received signal: “EXC_BAD_ACCESS”.

我有使用观察者实现的 viewDidLoad 方法:

- (void)viewDidLoad {
mapView = (MKMapView*)self.view;
mapView.delegate = self;
mapView.mapType = MKMapTypeHybrid;
mapView.showsUserLocation = YES;

// ...

[mapView.userLocation addObserver:self forKeyPath:@"location" options:0 context:NULL];
[super viewDidLoad];

}

我的释放:

- (void)dealloc {
[groupId release];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}

谁能告诉我这里出了什么问题吗?我单击导航栏中的后退按钮,然后进入 dealloc 方法,然后它切换回更高的 ViewController 并抛出此错误。

提前非常感谢并致以最诚挚的问候。

最佳答案

您正在不同对象上设置和删除观察者。您在第一个代码示例中将 View Controller 添加为 userLocation 的观察者,然后尝试在第二个代码示例中将其从默认通知中心中删除。要正确从 userLocation 中删除观察者,请将 -dealloc 方法更改为以下内容:

- (void)dealloc {
[groupId release];
[mapView.userLocation removeObserver:self forKeyPath:@"location"];
[super dealloc];
}

关于iPhone - 类的实例...正在被释放,而键值观察仍在其中注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2043469/

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