mickeyl / LTSupportAutomotive

An iOS / watchOS / macOS support library for OBD2, VIN-Decoding, and more.
MIT License
212 stars 59 forks source link

Disconnecting and Reconnecting to OBD2 adapter / Crash #27

Closed ron3001 closed 5 years ago

ron3001 commented 5 years ago

I've been using this library to establish BLE communication between iOS device and a vehicle for a while now - it helps a lot. Now I am getting a crash from the library, trying to disconnect the LTBTLESerialTransporter object. The crash is in:

-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
    LOG( @"Did disconnect %@: %@", peripheral, error );
    if ( peripheral == _adapter )
    {
        [_inputStream close];
        [_outputStream close];
    }
}

If there is established communication and the disconnect method is just called on the LTBTLESerialTransporter object (which is after that set to nil) there is no issue. But if the OBD2 adapter is disconnected from the vehicle and therefore the OBD2AdapterStateGone notification is received - on calling disconnect + setting the LTBTLESerialTransporter object to nil or just setting it to nil - the crash happens. The error is EXC_BAD_ACCESS KERN_INVALID_ADDRESS and happens on -[LTBTLEWriteCharacteristicStream close] . Is there more correct way to disconnect at this point - to avoid the crash and be able to reconnect after that? How is it intended to work?

mickeyl commented 5 years ago

Thanks for your report. The EXC_BAD_ACCESS was due to the delegate being invalid, setting this to weak should fix it. To show how I would handle a forceful disconnect/reconnect, I tweaked the example.

Could you check whether this works for you?

ron3001 commented 5 years ago

Thanks a lot for the demo tweak and change in the library! Tested with the example and there were no crashes there. My implementation also did not crash with the latest changes, but I will need to test more. I have one question. Since the crash was on [LTBTLEWriteCharacteristicStream close] shouldn't also the delegate there be changed to weak?

mickeyl commented 5 years ago

Absolutely, thanks. Closing this now – please reopen, if necessary.