2

I have a program that I cannot capture a random error and it crashes the process. I read the article on .NET 4 Framework not capturing System.AccessViolationException errors anymore unless you turn on a switch http://msdn.microsoft.com/en-us/library/dd638517(v=vs.110).aspx but it does not seem to work all the time. Has anyone had experience with this happening and how to capture these errors?

Here is the required switch I put in the the app.config:

<configuration>
  <runtime>
    <legacyCorruptedStateExceptionsPolicy enabled="true" />
  </runtime>
...

Here is the error report:

Log Name:      Application
Source:        .NET Runtime
Date:          8/19/2014 4:02:50 PM
Event ID:      1026
Task Category: None
Level:         Error
Keywords:      Classic
User:          N/A
Computer:      Test.local
Description:
Application: mainproc.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
Stack:
   at ADODB.RecordsetClass.Open(System.Object, System.Object, ADODB.CursorTypeEnum, ADODB.LockTypeEnum, Int32)
   at ADODB.RecordsetClass.Open(System.Object, System.Object, ADODB.CursorTypeEnum, ADODB.LockTypeEnum, Int32)
   at CEMain.clsThreadWritebackBatch.
(ADODB.Connection ByRef)
   at CEMain.clsThreadWritebackBatch.ProcessBatch()
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   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()

NEW:

The code below is where I think the problem may have occurred, I would value an opinion.

    Try
        OpenBackendDatabase(cn, False)
        ProcessQueue(cn)
    Catch
        Trace("ProcessBatch: " & Err.Description, True)
    Finally
        CloseBackendDatabase(cn)
        RaiseEvent FinishedBatch(123)
    End Try

So my thought was, if anything bad happens the FINALLY would close the database connection. The CloseBackendDatabase function has its own error handling. But now I am thinking if the "cn" reference was gone (pointer), then calling the ClockBackendDatabase function with a bad "cn" reference could cause an unhandled error? Does that make sense? If so, would this be a possible fix:

    Try
        OpenBackendDatabase(cn, False)
        ProcessQueue(cn)
    Catch
        Trace("ProcessBatch: " & Err.Description, True)
    Finally
        Try
            CloseBackendDatabase(cn)
        Catch
        End Try
        RaiseEvent FinishedBatch(123)
    End Try
Chris
  • 269
  • 2
  • 11
  • Thanks Heinzi, I looked at your reference and it is similar, their solution was to use the HandleProcessCorruptedStateExceptions, but from what I have read the app.config switch legacyCorruptedStateExceptionsPolicy covers the whole app. I think I will try that. I will post some additional code to see what people think. – Chris Aug 23 '14 at 16:22
  • 3
    As a side note: I don't know if this is possible for you or if you have too much legacy code, but you should really consider switching from ADODB to (at least) ADO.NET. ADODB is not meant to be used from .NET code, and ADO.NET is available since .NET 1.0. – Heinzi Aug 23 '14 at 16:25
  • Heinzi, totally agree and we are in the process of changing it over. We have a few clients that have problems in the present that we are at least trying to band-aid. Thanks for your comments. – Chris Aug 23 '14 at 16:30

1 Answers1

0

The easy fix is to check if the object exists before calling your close function:

If cn IsNot Nothing Then CloseBackendDatabase(cn)
The Blue Dog
  • 2,475
  • 3
  • 19
  • 25