【发布时间】:2014-01-19 19:37:58
【问题描述】:
我正在使用自动合成的@property。当我在方法中访问该属性时,我得到一个EXC_BAD_ACCESS 异常。以下是我的实现文件中的相关代码部分:
#import "BBBluetoothController.h"
#import <IOBluetooth/IOBluetooth.h>
@interface BBBluetoothController ()
@property (nonatomic, strong) CBCentralManager *bluetoothManager;
@end
@implementation BBBluetoothController
- (instancetype)init {
if (self = [super init]) {
_bluetoothManager = [[CBCentralManager alloc] init];
_bluetoothManager.delegate = self;
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
_bluetoothManager = [[CBCentralManager alloc] init];
_bluetoothManager.delegate = self;
}
return self;
}
- (IBAction)startScanning:(id)sender {
if (self.bluetoothManager.state == CBCentralManagerStatePoweredOn) {
[self.bluetoothManager scanForPeripheralsWithServices:nil options:nil];
self.isScanning = YES;
}
}
@end
在方法第一行的-startScanning: 中抛出异常。深入堆栈跟踪,我看到异常是从CBCentralManager 的实现内部抛出的:
0x7fff96c6ed49: leaq -413178944(%rip), %rax ; CBCentralManager._delegate
如果我闯入-startScanning:,我可以在lldb 中看到以下内容:
(lldb) po self
<BBBluetoothController: 0x1022213b0>
(lldb) po self.bluetoothManager
<CBConcreteCentralManager: 0x102222180>
(lldb) p self.bluetoothManager.state
(CBCentralManagerState) $2 = CBCentralManagerStateUnknown
(lldb) po self.bluetoothManager.delegate
<BBBluetoothController: 0x1022213b0>
此外,如果我引用 -startScanning: 中的实例变量而不是属性,那么一切都会顺利进行。我在这里遗漏了什么明显的东西吗?
编辑:
不管怎样,这在 OS X 10.9 上运行良好,但在 OS X 10.8.5 上运行良好。
【问题讨论】:
标签: macos memory-management properties exc-bad-access ivar