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