C#损坏的内存错误
时间:2020-03-05 18:41:06 来源:igfitidea点击:
我无法发布代码(专有问题),但是没有人知道哪种类型的事物会导致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:
更新:
原来,我们正在使用的一个库正在发送一个我们不知道的事件,而问题出在那儿。现在已修复。
解决方案
回答
如果我们要调用非托管代码(例如,一个dll。当编组错误非常严重时,可能会发生这种情况。
我们能否告诉我们我们是否正在调用非托管代码?如果是这样,我们是否使用默认的编组或者更具体的东西?从堆栈跟踪的外观来看,我们正在使用不安全的代码,例如指针之类的?这可能是问题。
回答
一些可能性的清单:
- 处置对象后,将使用该对象。如果要在终结器中放置托管对象,则可能会发生很多事情(不应该这样做)。
- 我们正在使用的对象之一的无人值守实现存在错误,并且破坏了进程内存堆。与DirectX,GDI等一起发生的事情很多。
- 在不受管理的边界上编组有缺陷。在代码的非托管部分使用托管指针之前,请确保将其固定。
- 我们正在使用不安全的块并对其进行有趣的操作。
在情况下,Windows窗体可能有问题。但是问题不在于它正在发生,而是在于它没有被正确报告。我们可能仍然做错了什么。
我们是否可以使用HWND来确定导致该错误的控件?总是一样吗?在应用程序崩溃之前,此控件是否在做一些有趣的事情?控件的无人管理部分是自定义窗口还是标准控件?
回答
这是更详细的堆栈跟踪。在我看来,它与System.Windows.Form.dll有关
TargetSite列为" {IntPtr DispatchMessageW(MSG ByRef)}"
在模块下有System.windows.forms.dll