1
我在Mac OSX上使用IOBluetooth框架来进行设备发现。我在回调函数中添加了一些NSLog()
,所以我知道进度。使用“-fobjc-arc”标记时出现分段错误
如果我编译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
导致无论是赛格故障:
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背景,所以如果可能的话,宁愿留在命令行上。
在此先感谢。
什么都没有坚持委托,因此它被释放? – Willeke
ARC不应该照顾这个吗? –
通过看到您没有强烈的引用,并为您解除分配对象,ARC正在“照顾该问题”。 – Ssswift