题
我无法发布代码(专有问题),但有谁知道什么类型的事情会导致 C# 中的以下错误。当另一个客户端结束呼叫时,我编写的 VOIP 客户端(使用 counterpath api)会抛出该错误。错误是:
System.AccessViolationException was unhandled
Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at CollabAnalysisSF.Edge.GUI.Forms.Program.Main() in d:\data\beyerss\Desktop\client\GUI\ARGui\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
更新:
事实证明,我们使用的一个库正在发送一个我们不知道的事件,而问题出在某个地方。现在修好了。
解决方案
一些可能性的列表:
- 对象在被处理后正在被使用。如果您在终结器中处置托管对象(您不应该这样做),则可能会经常发生这种情况。
- 您正在使用的对象之一的无人管理实现存在缺陷,并且损坏了进程内存堆。DirectX、GDI 等经常发生这种情况。
- 托管-非托管边界上的混编是有缺陷的。在代码的非托管部分使用托管指针之前,请确保先固定它。
- 您正在使用不安全的块并用它做有趣的事情。
在您的情况下,这可能是 Windows 窗体的问题。但问题不在于它正在发生,而在于它没有得到正确的报道;你可能仍然做错了什么。
您是否能够使用 HWND 确定哪个控件导致错误?总是一样吗?这个控件在应用程序崩溃之前做了一些有趣的事情吗?控件的非托管部分是自定义窗口还是标准控件?
其他提示
如果您调用非托管代码,例如,可能会出现此类问题一个dll。当编组出现严重错误时,就会发生这种情况。
您能告诉我们您是否正在调用非托管代码吗?如果是这样,您使用的是默认编组还是更具体的东西?从堆栈跟踪的外观来看,您是否使用了不安全的代码,例如指针之类的?这可能是你的问题。
这是更详细的堆栈跟踪。在我看来它与 System.Windows.Form.dll 有关
TargetSite 被列为 {IntPtr DispatchMessageW(MSG ByRef)}
在模块下有 System.windows.forms.dll
不隶属于 StackOverflow