Crash of UWP's HID library

0

I am developing a UWP application to work with HID devices. The application is working correctly almost all the time. But it often crashes when I close or collapse/restore. I got exception Access violation reading location:

Exception thrown at 0x5FC8A31C (Windows.Devices.HumanInterfaceDevice.dll) in MyApp.exe: 0xC0000005: Access violation reading location 0x0000001E.

And this exception always appears on CreateOutputReport method.

How can I check the possibility of the creation of the output report? Or how can I handle this exception in UWP?

Thanks for any advice in advance.

UPD: Here is a code sample

HidDevice device = await HidDevice.FromIdAsync(id, FileAccessMode.ReadWrite);
if(device != null)
{
    var outReport = device.CreateOutputReport(); // Crashes here only on collapsing/closing
    byte[] buffer = new byte[(int)outReport.Data.Capacity];
    outReport.Data = buffer.AsBuffer();
    await device.SendOutputReportAsync(outReport);
}

UPD 2: Tested from scratch. Created UWP app with this code:

public async void StartSending()
{
    var devices = await DeviceInformation.FindAllAsync(HidDevice.GetDeviceSelector(usagePage, usageId, vid, pid));

    if (devices.Any())
    {
        var device = devices.FirstOrDefault();

        HidDevice hidDevice = await HidDevice.FromIdAsync(device.Id, FileAccessMode.ReadWrite);

        if (hidDevice != null)
        {
            while(true)
            {
                var outReport = hidDevice.CreateOutputReport();

                byte[] buffer = new byte[(int)outReport.Data.Capacity];

                // fill data

                outReport.Data = buffer.AsBuffer();
                await hidDevice.SendOutputReportAsync(outReport);

                await Task.Delay(500);
            }
        }
    }
}

Everything works great until I collapse the application. Application failed after restoring. But this appears only on the installed app. It works correctly from VS.

Video

c#
uwp
hid
asked on Stack Overflow Oct 9, 2018 by Maksym • edited Oct 10, 2018 by Maksym

1 Answer

0

Thanks for all responses. @XavierXie-MSFT was right, I haven't any logic for suspending/resuming. It works now.

Here is code:

Application.Current.Suspending += OnSuspending;
Application.Current.Resuming += OnResuming;

private void OnResuming(object sender, object e)
{
    // Reinitialize all watchers for HID
    // Subscribe to all necessary events
}

private void OnSuspending(object sender, SuspendingEventArgs e)
{
    // Remove all watchers for HID
    // Unsubscribe from all events
    // Dispose all active HID devices
}
answered on Stack Overflow Oct 19, 2018 by Maksym

User contributions licensed under CC BY-SA 3.0