使用标志"-fobjc-arc"时出现分段错误



我正在使用Mac OSX上的IOBluetooth框架进行设备发现。我在回调函数中添加了一些NSLog(),所以我知道进度。

如果我像这样在命令行上使用 gcc编译代码,一切正常(源文件f.m,输出a):

gcc -o a f.m -framework Foundation -framework IOBluetooth

但是,如果我添加一个-fobjc-arc标志以确保自动引用计数:

gcc -fobjc-arc -o a f.m -framework Foundation -framework IOBluetooth

编译仍然可以,但执行文件./a导致 seg 错误:

2017-06-21 22:06:23.150 a[718:17437] Program started ...
Segmentation fault: 11

或永远挂在那里:

2017-06-21 22:06:27.070 a[721:17809] Program started ...

有时它挂起,有时它断层叠叠。行为不一致。如果我不添加-fobjc-arc标志,一切都按预期工作(至少在表面上)。

所以我的问题是,为什么在我添加-fobjc-arc标志后它的行为方式?

如果有帮助,完整的源文件在这里:

#import <IOBluetooth/IOBluetooth.h>
@interface InquiryDelegate : NSObject <IOBluetoothDeviceInquiryDelegate>
@end
@implementation InquiryDelegate
-(void)deviceInquiryStarted:(IOBluetoothDeviceInquiry *)sender
{
NSLog(@"Inquiry started ...");
}
-(void)deviceInquiryDeviceFound:(IOBluetoothDeviceInquiry *)sender
device:(IOBluetoothDevice *)device
{
NSLog(@"Device found");
}
-(void)deviceInquiryComplete:(IOBluetoothDeviceInquiry *)sender
error:(IOReturn)error
aborted:(BOOL)aborted
{
NSLog(@"Inquiry complete");
}
-(void)deviceInquiryUpdatingDeviceNamesStarted:(IOBluetoothDeviceInquiry *)sender
devicesRemaining:(uint32_t)devicesRemaining
{
}
-(void)deviceInquiryDeviceNameUpdated:(IOBluetoothDeviceInquiry *)sender
device:(IOBluetoothDevice *)device
devicesRemaining:(uint32_t)devicesRemaining
{
}
@end
int main(int argc, const char* argv[]) {
@autoreleasepool {
NSLog(@"Program started ...");
IOBluetoothDeviceInquiry* di = [[IOBluetoothDeviceInquiry alloc]
initWithDelegate:[[InquiryDelegate alloc] init]];
[di start];
[[NSRunLoop currentRunLoop] run];
}
}

如果有人想知道,我的目标是生成一个用于 JNI 项目的动态库,不涉及 GUI。这是我试图获得一些关于Mac上蓝牙方式的经验,并让自己熟悉Objective-C。我来自 Linux 背景,所以如果可能的话,我更喜欢留在命令行上。

提前谢谢。

感谢上面评论者的提示,我能够通过添加对委托对象的强引用来解决问题:

InquiryDelegate* g = [[InquiryDelegate alloc] init];
IOBluetoothDeviceInquiry* di = [[IOBluetoothDeviceInquiry alloc]
initWithDelegate:g];

感谢@Willeke和@Ssswift。

最新更新