altera2015 / usbserial

Flutter Android USB Serial plugin
BSD 3-Clause "New" or "Revised" License
121 stars 83 forks source link

App forced to close #27

Closed berdroid closed 4 years ago

berdroid commented 4 years ago

Running on Android 6.0.1 my app was closed by the system.

Android LogCat showed an null-reference exception at UsbSerialPortAdapter.java:67:

02-11 16:46:10.849 5944-5944/com.bytecmed.custom_slider D/AndroidRuntime: Shutting down VM
02-11 16:46:10.851 5944-5944/com.bytecmed.custom_slider E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.bytecmed.custom_slider, PID: 5944
    java.lang.NullPointerException: **Attempt to invoke interface method 'void io.flutter.plugin.common.EventChannel$EventSink.success(java.lang.Object)' on a null object reference**
        at dev.bessems.usbserial.UsbSerialPortAdapter$1$1.run(UsbSerialPortAdapter.java:67)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-11 16:47:15.405 5944-5944/com.bytecmed.custom_slider I/Process: Sending signal. PID: 5944 SIG: 9
berdroid commented 4 years ago

I think the exception was caused by a race condition between the onReceiveData() method and the onCancel method setting m_EventSink to null.

Possible fix: before actually calling m_EventSink.success(arg0) (line 67) the run() method should check for m_EventSink != null again, as it may have been set to null in the mean time.

berdroid commented 4 years ago

This seems to be easily provoked by plugging/unplugging a USB serial adapter several times