Objective c 使用标志“-fobjc arc”时出现分段错误
我正在Mac OSX上使用IOBluetooth框架进行设备发现。我在回调函数中添加了一些Objective c 使用标志“-fobjc arc”时出现分段错误,objective-c,macos,bluetooth,automatic-ref-counting,iobluetooth,Objective C,Macos,Bluetooth,Automatic Ref Counting,Iobluetooth,我正在Mac OSX上使用IOBluetooth框架进行设备发现。我在回调函数中添加了一些NSLog() 如果我像这样在命令行上用gcc编译代码,一切正常(源文件f.m,输出a): 但是,如果我添加-fobjc arc标志以确保自动参考计数: gcc -fobjc-arc -o a f.m -framework Foundation -framework IOBluetooth 编译仍然正常,但执行文件/a会导致seg故障: 2017-06-21 22:06:23.150 a[718:1743
NSLog()
如果我像这样在命令行上用gcc编译代码,一切正常(源文件f.m
,输出a
):
但是,如果我添加-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];
}
}
#导入
@接口查询委托:NSObject
@结束
@实现查询委托
-(void)设备查询开始:(IOBluetoothDeviceInquiry*)发送方
{
NSLog(@“查询已开始…”);
}
-(void)deviceInquiryDeviceFound:(IOBluetoothDeviceInquiry*)发送方
设备:(IOBluetoothDevice*)设备
{
NSLog(@“找到设备”);
}
-(无效)设备查询完成:(IOBluetooth设备查询*)发送方
错误:(IOReturn)错误
中止:(BOOL)中止
{
NSLog(“查询完成”);
}
-(无效)设备查询更新设备名称已启动:(IOBluetooth设备查询*)发送方
设备剩余:(uint32_t)设备剩余
{
}
-(无效)设备查询设备更新:(IOBluetoothDeviceInquiry*)发送方
设备:(IOBluetoothDevice*)设备
设备剩余:(uint32_t)设备剩余
{
}
@结束
int main(int argc,const char*argv[]{
@自动释放池{
NSLog(@“程序已启动…”);
IOBluetoothDeviceInquiry*di=[[IOBluetoothDeviceInquiry alloc]
initWithDelegate:[[InquiryDelegate alloc]init]];
[di start];
[[nsrunlop currentRunLoop]run];
}
}
如果有人想知道,我的目标是生成一个用于JNI项目的动态库,而不涉及GUI。这是我尝试在Mac上使用蓝牙的方式获得一些经验,并让自己熟悉Objective-C。我来自Linux背景,因此如果可能的话,我更愿意使用命令行
提前感谢。感谢以上评论员的提示,我能够通过添加对委托对象的强引用来解决问题:
InquiryDelegate* g = [[InquiryDelegate alloc] init];
IOBluetoothDeviceInquiry* di = [[IOBluetoothDeviceInquiry alloc]
initWithDelegate:g];
多亏了@Willeke和@Ssswift。多亏了上述评论者的提示,我能够通过添加对委托对象的强引用来解决问题:
InquiryDelegate* g = [[InquiryDelegate alloc] init];
IOBluetoothDeviceInquiry* di = [[IOBluetoothDeviceInquiry alloc]
initWithDelegate:g];
多亏了@Willeke和@Ssswift。没有任何东西能抓住代理,因此它被解除分配了?ARC不应该为我处理这些吗?ARC正在“处理这些”,因为你看不到它的有力证据,并为您解除分配对象。没有任何东西会抓住委托,因此它被解除分配?ARC不应该为我处理这个问题吗?ARC通过看到您没有对它的强引用来“处理这个问题”,并为您解除分配对象。