[英]“EXEC_BAD_ACCESS” error when reading NSUserDefaults
I'm having an error occur when I read the NSUserDefaults (via InAppSettingsKit). 读取NSUserDefaults(通过InAppSettingsKit)时出现错误。 I'm not sure if it's my code that's the issue though.
我不确定这是否是我的代码。 I have set up an observer to check if there are any changes to the NSUserDefaults:
我已经设置了一个观察者来检查NSUserDefaults是否有任何变化:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(setOptions)
name:NSUserDefaultsDidChangeNotification
object:nil];
The method this calls is used to update the 'map type' of an MKMapView: 此调用的方法用于更新MKMapView的“地图类型”:
- (void)setOptions
{
// Get the map style
NSString *mapStyle = [[NSUserDefaults standardUserDefaults] stringForKey:kMapType];
// Update map style
if ([mapStyle isEqualToString:@"Hybrid"])
{
map.mapType = MKMapTypeHybrid;
}
else if ([mapStyle isEqualToString:@"Map"])
{
map.mapType = MKMapTypeStandard;
}
else if ([mapStyle isEqualToString:@"Satellite"])
{
map.mapType = MKMapTypeSatellite;
}
[mapStyle release];
}
The app is set up such that you press a button and the InAppSettingsKit is initialised, within this I change the setting for the map type to be displayed and go back to the main screen in my app. 设置应用程序时,您可以按一下按钮并初始化InAppSettingsKit,在此过程中,我更改了要显示的地图类型的设置,并返回到应用程序的主屏幕。 At this point the map seems to update correctly and there are no issues.
此时,地图似乎已正确更新,并且没有问题。 The issue occurs when I try to re-launch the InAppSettingsKit to change the map type again.
当我尝试重新启动InAppSettingsKit以再次更改地图类型时,会发生此问题。
Does anyone know if it's my code that's the issue, if so how can I go about fixing it? 有谁知道是我的代码是问题,如果是的话,我该如何解决呢?
just remove the code-line: [mapStyle release]
只需删除代码行即可:
[mapStyle release]
the stringForKey:
will return an autoreleased NSString
. stringForKey:
将返回一个自动释放的NSString
。 So your code is not responsible for releasing. 因此,您的代码不负责发布。 It works fine in the first iteration because the first release call will dealloc that string but the
NSUserDefaults
still has a pointer to that String but is not used. 它在第一个迭代中运行良好,因为第一个发布调用将
NSUserDefaults
该字符串,但NSUserDefaults
仍然具有指向该字符串的指针,但未使用。 In the second iteration you get that pointer and try to call isEqualToString
on that dealloc-ed object wich will cause the BAD_ACCESS 在第二个迭代中,您将获得该指针,并尝试在该已分配对象上调用
isEqualToString
,这将导致BAD_ACCESS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.