将CLBeacon设置为nil时为EXC_BAD_ACCESS [英] EXC_BAD_ACCESS when setting a CLBeacon to nil

查看:88
本文介绍了将CLBeacon设置为nil时为EXC_BAD_ACCESS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将 CLBeacon 设置为 nil 。

  CLBeacon * beacon = [[CLBeacon分配]在里面]; 
beacon = nil; //崩溃

是否不可能取消分配已初始化的 CLBeacon






只需将上面的代码添加到App Delegate的中的一个新项目中,即可复制此代码didFinishLaunchingWithOptions

 -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
CLBeacon * beacon = [[CLBeacon alloc] init];
beacon = nil; //崩溃
返回YES;
}


解决方案

CLBeacon 状态:


您不直接创建此类的实例。位置管理器对象将遇到的信标报告给与其关联的委托对象。


崩溃的原因是实现细节,实际上并不重要,但是这是由于当您仅调用 init CLBeacon s没有正确初始化的事实。取消分配时, CLBeacon 取消引用它的 _internal ivar,如果它为 NULL



您可以通过查看 CLBeacon-> _ internal ivar的值来查看在调试器中。如果使用 init 创建信标,则ivar为 NULL ,但是如果使用 [[[CLBeacon alloc] initWithCoder:nil] ,它将具有一个值,并且在将信标设置为 nil 时不会崩溃。 / p>

The following will perform a crash when setting CLBeacon to nil.

CLBeacon *beacon = [[CLBeacon alloc] init];
beacon = nil; // crash

Is it not possible to deallocate an initialized CLBeacon?


This can be reproduced by simply adding the code above to a fresh project inside the App Delegate's didFinishLaunchingWithOptions

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    CLBeacon *beacon = [[CLBeacon alloc] init];
    beacon = nil; // crash
    return YES;
}

解决方案

The apple documentation for CLBeacon states:

You do not create instances of this class directly. The location manager object reports encountered beacons to its associated delegate object.

The reason it crashes is an implementation detail that doesn't really matter, but it is due to the fact that CLBeacons are not properly initialized when you just call init. When it deallocates, CLBeacon dereferences it's _internal ivar and crashes if it is NULL.

You can see this by looking at the value of the CLBeacon->_internal ivar in the debugger. If you create the beacon using init then the ivar is NULL, but if you create it with [[CLBeacon alloc] initWithCoder:nil] it will have a value and it doesn't crash when you set the beacon to nil.

这篇关于将CLBeacon设置为nil时为EXC_BAD_ACCESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆