PORTS Forum
Ports and Interfaces => USB => Topic started by: ganzziani on August 09, 2012, 03:24:37 pm
-
I generally get an exception when I close the program:
AccessViolationException was unhandled.
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
The exception occurs in this line of code:
WinUsb_Free(myDevInfo.winUsbHandle);
This hasn't been that bad for me, it only occurs when debugging the code. The final executable doesn't show errors.
But it would be nice to know what is going on...
-
Just off the top of my head, does the code first check to see if the handle exists and is closed?
-
The handle check occurs afterward:
internal void CloseDeviceHandle() {
try {
WinUsb_Free(myDevInfo.winUsbHandle);
if (!(myDevInfo.deviceHandle == null)) {
if (!(myDevInfo.deviceHandle.IsInvalid)) {
myDevInfo.deviceHandle.Close();
}
}
}
catch (Exception ex) {
throw;
}
}
-
My application continuously does bulk read transfers. If I don't initiate the reads, the exception doesn't occur.
So, when closing the application, it's possible that a transfer could be pending.
How can I abort the transfer before closing the application?
I tried: WinUsb_AbortPipe(myDevInfo.winUsbHandle, myDevInfo.bulkInPipe);
But doesn't seem to work.
A temp workaround is to have a delay before exit, this gives time for the ongoing transfer to complete, but this will not work all the time...
-
This isn't elegant either, and there may be a better way, but you could set a variable on starting a transfer, reset the variable on completion, and check the variable and wait as needed on closing the application.
Or catch and ignore the exception.