程序终止后抛出异常(遇到的错误)

Per*_*ion 5 c# vb.net unsafe

我有一个应用程序,似乎只有在程序关闭后才会抛出异常.这是非常不一致的.(我们都知道不一致的bug有多么有趣......)

我的猜测是在清理过程中出现错误.但是这些内存读/写错误似乎表明我的"不安全"代码使用(指针?)有问题.

我感兴趣的是调试这些情况的最佳方法是什么?
你如何调试已经关闭的程序?
我正在寻找一个解决更大问题的起点.

这些错误似乎以多种方式呈现(一些运行时,一些调试):

1: .NET-BroadcastEventWindow.2.0.0.0.378734a.0:  Application.exe - Application Error
The instruction at "0x03b4eddb" referenced memory at "0x00000004". The memory could not be "written". 2: Application.vshost.exe - Application Error
The instruction at "0x0450eddb" referenced memory at "0x00000004". The memory could not be "written". 3: Application.vshost.exe - Application Error
The instruction at "0x7c911669" referenced memory at "0x00000000". The memory could not be "read". 4: Application.vshost.exe - Application Error
The instruction at "0x7c910ed4" referenced memory at "0xfffffff8". The memory could not be "read".

Mir*_*vec 6

我使用AcrobarReader COM组件时遇到此问题.在应用程序退出后不时出现"Application.vshost.exe - Application Error""无法读取内存".GC.Collect()和WaitForPendingFinalizers()没有帮助.

我的google-fu带我到这个页面:http://support.microsoft.com/kb/826220.我为我的案子修改了方法3.

使用进程资源管理器我发现AcroPDF.dll没有在Main函数的最后一行之前发布.所以,这里是API调用.

DLLImports(DLLImport在System.Runtime.InteropServices命名空间中):

<DllImport("kernel32.dll", EntryPoint:="GetModuleHandle", _
       SetLastError:=True, CharSet:=CharSet.Auto, _
       CallingConvention:=CallingConvention.StdCall)> _
Public Overloads Shared Function GetModuleHandle(ByVal sLibName As String) As IntPtr
End Function

<DllImport("kernel32.dll", EntryPoint:="FreeLibrary", _
    SetLastError:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Overloads Shared Function FreeLibrary(ByVal hMod As IntPtr) As Integer
End Function
Run Code Online (Sandbox Code Playgroud)

然后在申请退出之前:

Dim hOwcHandle As IntPtr = GetModuleHandle("AcroPDF.dll")
If Not hOwcHandle.Equals(IntPtr.Zero) Then
    FreeLibrary(hOwcHandle)
    Debug.WriteLine("AcroPDF.dll freed")
End If
Run Code Online (Sandbox Code Playgroud)

可以针对任何其他不良行为的dll修改此过程.我只是希望它不会引入任何新的错误.


Rob*_*len 4

如果您的应用程序是多线程的,您可能会从工作线程中收到错误,这些线程未正确终止并尝试访问已处置的对象。