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.
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
}
User contributions licensed under CC BY-SA 3.0