VB.NET中未处理的访问冲突

时间:2014-08-23 16:09:07

标签: vb.net adodb

我有一个程序,我无法捕获随机错误,它会使进程崩溃。我阅读有关.NET 4 Framework的文章不再捕获System.AccessViolationException错误,除非你打开一个开关http://msdn.microsoft.com/en-us/library/dd638517(v=vs.110).aspx但它似乎不能一直工作。有没有人有过这种情况的经验以及如何捕获这些错误?

以下是我在app.config中输入的所需开关:

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

以下是错误报告:

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:

下面的代码是我认为问题可能发生的地方,我会重视意见。

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

所以我的想法是,如果发生任何不好的事情,最终将关闭数据库连接。 CloseBackendDatabase函数有自己的错误处理。但现在我在想,如果&#34; cn&#34;引用已经消失(指针),然后用坏的&#34; cn&#34;调用ClockBackendDatabase函数。引用可能导致未处理的错误?那有意义吗?如果是这样,这可能是一个可能的解决办法:

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

1 个答案:

答案 0 :(得分:0)

简单的解决方法是在调用close函数之前检查对象是否存在:

If cn IsNot Nothing Then CloseBackendDatabase(cn)