我正在开发一个iOS应用,该应用通过OBD2适配器在iOS设备和车辆之间建立通信。为此,我使用了一个库-LTSupportAutomotive。沟通良好。当适配器从车辆上断开连接并因此中断连接时,就会出现问题。有一阵子我崩溃了。我有一个Crashlytics崩溃报告,错误是EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x00000008ba9ae0a0。从我阅读的内容来看,这意味着有一个指向无效内存的指针。我真的不明白为什么会发生这种情况以及如何进行解决。崩溃是通过库中的以下方法发生的:
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
LOG( @"Did disconnect %@: %@", peripheral, error );
if ( peripheral == _adapter )
{
[_inputStream close];
[_outputStream close];
}
}
有一条连接中断的通知。当我处理它时,我将变量指向具有以上方法的类(LTBTLESerialTransporter
)的对象nil
。这应调用其dealloc
方法,其中应调用方法disconnect
:
-(void)disconnect
{
[self stopUpdatingSignalStrength];
[_inputStream close];
[_outputStream close];
if ( _adapter )
{
[_manager cancelPeripheralConnection:_adapter];
}
[_possibleAdapters enumerateObjectsUsingBlock:^(CBPeripheral * _Nonnull peripheral, NSUInteger idx, BOOL * _Nonnull stop) {
[_manager cancelPeripheralConnection:peripheral];
}];
}
崩溃报告如下:
崩溃:LTBTLESerialTransporter
0 libobjc.A.dylib 0x1d35a3bf0 objc_retain + 16
1 LTSupportAutomotive 0x100d58040-[LTBTLEWriteCharacteristicStream close] + 80
2 LTSupportAutomotive 0x100d56c04-[LTBTLESerialTransporter centralManager:didDisconnectPeripheral:error:] + 264
3 CoreBluetooth 0x1d9e423ec-[CBCentralManager handlePeripheralDisconnectionCompleted:] + 232
4 CoreBluetooth 0x1d9e431e8-[CBCentralManager handleMsg:args:] + 620
5 CoreBluetooth 0x1d9e51430 __30- [CBXpcConnection _handleMsg:] _ block_invoke + 60
6 libdispatch.dylib 0x1d3df0a38 _dispatch_call_block_and_release + 24
7 libdispatch.dylib 0x1d3df17d4 _dispatch_client_callout + 16
8 libdispatch.dylib 0x1d3d9a320 _dispatch_lane_serial_drain $ VARIANT $ mp + 592
9 libdispatch.dylib 0x1d3d9ae70 _dispatch_lane_invoke $ VARIANT $ mp + 480
10 libdispatch.dylib 0x1d3d9a1ec _dispatch_lane_serial_drain $ VARIANT $ mp + 284
11 libdispatch.dylib 0x1d3d9ae3c _dispatch_lane_invoke $ VARIANT $ mp + 428
12 libdispatch.dylib 0x1d3da34a8 _dispatch_workloop_worker_thread + 596
13 libsystem_pthread.dylib 0x1d3fd1114 _pthread_wqthread + 304
14 libsystem_pthread.dylib 0x1d3fd3cd4 start_wqthread + 4