none
Unhandled exception: System.AccessViolationException RRS feed

  • Question

  • I was testing something in our test environment and received an error. This code is running in production and executes about 100,000 times a day and we don't see this problem but we do have some similar file access errors that are handled. Here is the error message from the application log. It is from opening a text file and it is in a Try Catch block and this is a Windows service I have written. Also, this is being executed in a thread but that shouldn't matter.

    Application: PXAgentService.exe
    Framework Version: v4.0.30319
    Description: The process was terminated due to an unhandled exception.
    Exception Info: System.AccessViolationException
    Stack:
       at Microsoft.Win32.Win32Native.SetErrorMode(Int32)
       at System.IO.FileStream.Init(System.String, System.IO.FileMode, System.IO.FileAccess, Int32, Boolean, System.IO.FileShare, Int32, System.IO.FileOptions, SECURITY_ATTRIBUTES, System.String, Boolean, Boolean)
       at System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, Int32, System.IO.FileOptions)
       at System.IO.StreamReader..ctor(System.String, System.Text.Encoding, Boolean, Int32)
       at System.IO.StreamReader..ctor(System.String)
       at System.IO.File.OpenText(System.String)
       at PXAgentService.clsPrint.MergeForPageCount(System.String, System.String)
       at PXAgentService.clsPrint.RunForPageCount(System.String)
       at PXAgentService.clsPrint.MergeThread()
       at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
       at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
       at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
       at System.Threading.ThreadHelper.ThreadStart()

    • Moved by 宝宝徐 Wednesday, August 9, 2017 7:11 AM
    Tuesday, August 8, 2017 9:17 PM

All replies

  • Hi Scottb7711,

    Thank you for posting in MSDN forum.

    This forum is about .Net Framework Setup and Servicing, from your description, this issue is much related to the CLR, I help you move this case to CLR to get a better professional support.

    Thank you for your understanding and cooperation.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 9, 2017 7:11 AM
  • Hi Scottb7711,

    How do you open the file? Using File.OpenText Method?

    An AccessViolationException exception can occur only in unsafe managed code or when verifiable managed code interacts with unmanaged code:

    • An access violation that occurs in unsafe managed code can be expressed as either a NullReferenceException exception or an AccessViolationException exception, depending on the platform.
    • An access violation in unmanaged code that bubbles up to managed code is always wrapped in an AccessViolationException exception.

    In either case, you can identify and correct the cause of the AccessViolationException exception as follows:

    • Make sure that the memory that you are attempting to access has been allocated.
    • Make sure that the memory that you are attempting to access has not been corrupted.

    For more details, please refer to the following link.

    https://msdn.microsoft.com/en-us/library/system.accessviolationexception(v=vs.110).aspx

    https://msdn.microsoft.com/en-us/library/ms164911.aspx

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Sunday, August 27, 2017 3:07 PM
    Moderator
  • I am using the File.OpenText method. This is written in VB.Net as well. I still don't understand why it bypassed my error handler. It appears that the objects in the process are being created and destroyed correctly as evident from the number of executions performed routinely without any memory leaks or issues.
    Tuesday, August 29, 2017 4:54 PM
  • I am using the File.OpenText method. This is written in VB.Net as well. I still don't understand why it bypassed my error handler. It appears that the objects in the process are being created and destroyed correctly as evident from the number of executions performed routinely without any memory leaks or issues.

    The native SetErrorMode may be attempting to display or refer to a MessageBox which may cause a serious failure in a Windows Service. 

    You can see here that the FileStream.cs file does call SetErrorMode on lines 840 and 886. These are the only places it calls it too so we know that your application definitely fails on one of these calls.

    It may well be the call on line 886 that fails as this may try to restore the default mode which is to display a message box, perhaps somewhere in this area it faults because  a service has no GUI thread?

    You could try - as a test - to enable the service to interact with desktop and see if it still fails? 

    If you use the SCM to start/stop the service then go to the Log On tab, you'll see a checkbox: Allow service to interact with desktop - set this and restart, see it it stops failing.

    If it does then this may be your only workaround...



    Monday, September 4, 2017 8:46 PM