felHR85 / UsbSerial

Usb serial controller for Android
MIT License
1.8k stars 585 forks source link

Thread leak #237

Open GuiRitter opened 5 years ago

GuiRitter commented 5 years ago

Hi everyone.

I found out that, whenever the USB cable is disconnected and reconnected and UsbSerialDevice.createUsbSerialDevice is called again, the threads created by the previous call remain running and new ones are created. It seems that, on each call, the number of created threads increase, such that they pile up quite quickly after a few calls.

After some research (maybe related), I thought that calling UsbSerialDevice.close would be enough, but it wasn't.

Digging deeper, I downloaded the source code and used it in my project. Since I'm using a CH34x device, I edited CH34xSerialDevice and added the following method:

    @Override
    protected void finalize() throws Throwable {
        close();
        super.finalize();
    }

That fixed it without having to call UsbSerialDevice.close. Only one thread remained running at once.

I don't have other devices to test if the issue exists with the other classes or, if it does, if this patch fixes it, so please take a look into it.

I tested with an Android 7.1.1.

Thanks in advance.

felHR85 commented 5 years ago

I will take a look. Thank you very much for reporting this