Jinjinov / Usb.Events

Subscribe to the Inserted and Removed events to be notified when a USB drive is plugged in or unplugged, or when a USB device is connected or disconnected. Usb.Events is a .NET Standard 2.0 library and uses WMI on Windows, libudev on Linux and IOKit on macOS.
MIT License
95 stars 23 forks source link

Disposing UsbEventWatcher on Linux throws a null reference exception. #14

Closed thomOrbelius closed 3 years ago

thomOrbelius commented 3 years ago

Library version 1.1.1.1

OS & OS version Ubuntu Server 20.04 LTS

Describe the bug When disposing UsbEventWatcher on Linux we get a null reference exception.

System.NullReferenceException : Object reference not set to an instance of an object. at Usb.Events.UsbEventWatcher.Dispose()

To Reproduce Steps to reproduce the behavior:

  1. Create a disposable class with the UsbEventWatcher in it and dispose it (see code below)

(We used Dependency Injection when we found it, but it's easily reproducible by just calling the dispose method)

internal class Dummy : IDisposable
{
    private readonly UsbEventWatcher _usbEventWatcher = new();
    private bool _disposedValue;

    protected virtual void Dispose(bool disposing)
    {
        if (!_disposedValue)
        {
            if (disposing)
            {
                _usbEventWatcher.Dispose();
            }

            _disposedValue = true;
        }
    }
}

Expected behavior The Dispose method should not throw any null reference exceptions.

Additional context I think it's the Windows specific objects _volumeChangeEventWatcher, _USBControllerDeviceCreationEventWatcher, _USBControllerDeviceDeletionEventWatcher that throws the exception as they are not created on the other platforms. A simple null check per object or a Windows specific dispose section should fix it.

I do have one concern about this - are there any Linux/Mac specific objects that also needs to be disposed here?

Jinjinov commented 3 years ago

Thank you, I will look into this as soon as I have time, hopefully in a few days.

Regarding the disposal of any Linux/Mac specific objects

https://github.com/Jinjinov/Usb.Events/blob/master/Usb.Events/UsbEventWatcher.Linux.c https://github.com/Jinjinov/Usb.Events/blob/master/Usb.Events/UsbEventWatcher.Mac.c

The code for Linux and Mac is pure C, because it is not possible to do this in C#

https://github.com/Jinjinov/Usb.Events/blob/master/Usb.Events/Usb.Events.csproj#L34

as you can see here, the project file compiles the C code to .so on Linux and to .dylib on Mac

As I am not a C programmer, I wrote the C code with lots and lots of Stackoverflow help - I would say that there is no need to dispose of any C objects, but I am not 100% certain.

thomOrbelius commented 3 years ago

I made a simple fix, where the objects are null checked before disposing.

I have not been able to test in for Mac, but the updated version of the test application should be easy to use.

Jinjinov commented 3 years ago

Thank you!