Author Topic: Exception on exit: AccessViolationException  (Read 10368 times)

ganzziani

  • Member
  • ***
  • Posts: 4
Exception on exit: AccessViolationException
« on: August 09, 2012, 03:24:37 pm »
I generally get an exception when I close the program:

Quote
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...

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Exception on exit: AccessViolationException
« Reply #1 on: August 09, 2012, 05:39:09 pm »
Just off the top of my head, does the code first check to see if the handle exists and is closed?

ganzziani

  • Member
  • ***
  • Posts: 4
Re: Exception on exit: AccessViolationException
« Reply #2 on: August 09, 2012, 08:16:35 pm »
The handle check occurs afterward:

Code: [Select]
internal void CloseDeviceHandle() {
try {
WinUsb_Free(myDevInfo.winUsbHandle);
if (!(myDevInfo.deviceHandle == null)) {
if (!(myDevInfo.deviceHandle.IsInvalid)) {
myDevInfo.deviceHandle.Close();
}
}
}
catch (Exception ex) {
throw;
}
}

ganzziani

  • Member
  • ***
  • Posts: 4
Re: Exception on exit: AccessViolationException
« Reply #3 on: August 09, 2012, 10:17:04 pm »
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...
« Last Edit: August 09, 2012, 10:57:46 pm by ganzziani »

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Exception on exit: AccessViolationException
« Reply #4 on: August 10, 2012, 06:49:39 pm »
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.
« Last Edit: August 10, 2012, 06:52:16 pm by Jan Axelson »