C# 损坏内存错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Corrupt Memory Error
提问by Adam Lerman
I cant post the code (proprietary issues) but does anyone know what types of things would cause the following error in C#. It is being thrown by a VOIP client that I wrote (using counterpath api) when the call is ended by the other client. The error is:
我不能发布代码(专有问题),但有没有人知道什么类型的东西会导致 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:
UPDATE:
Turns out one of the libraries we were using was sending off an event that we didnt know about, and the problem was in there somewhere. Fixed now.
更新:
原来我们使用的一个库正在发送一个我们不知道的事件,问题出在某个地方。现在固定。
采纳答案by Coincoin
List of some possibilities:
一些可能性的列表:
- An object is being used after it has been disposed. This can happen a lot if you are disposing managed object in a finalizer (you should not do that).
- An unmannaged implementation of one of the object you are using is bugged and it corrupted the process memory heap. Happens a lot with DirectX, GDI and others.
- Mashaling on managed-unmanaged boundary is flawed. Make sure you pin a managed pointer before you use it on an unmanaged part of code.
- You are using unsafe block and doing funny stuff with it.
- 一个对象在被处理后正在被使用。如果您在终结器中处理托管对象(您不应该这样做),这可能会发生很多。
- 您正在使用的对象之一的无人管理实现被窃听,它破坏了进程内存堆。DirectX、GDI 等经常发生这种情况。
- 在托管-非托管边界上混搭是有缺陷的。在将托管指针用于代码的非托管部分之前,请确保将其固定。
- 您正在使用 unsafe 块并用它做有趣的事情。
In you case it could be a problem with Windows Forms. But the problem is not that it is happening, but rather that it is not being reported correctly; you possibly still have done something wrong.
在您的情况下,这可能是 Windows 窗体的问题。但问题不在于它正在发生,而在于它没有被正确报告;你可能还是做错了什么。
Are you able to determine what control is causing the error using the HWND? Is it always the same? Is this control doing something funny just before the application crashes? Is the unmannaged part of the control a custom window or a standard control?
您是否能够使用 HWND 确定导致错误的控件?它总是一样吗?这个控件在应用程序崩溃之前做了什么有趣的事情吗?控件的非托管部分是自定义窗口还是标准控件?
回答by TK.
This kind of prolem can occur if you are calling unmanaged code e.g. a dll. It can occur when Marshalling goes horribly wrong.
如果您正在调用非托管代码(例如 dll),则可能会出现这种问题。当编组出现严重错误时,就会发生这种情况。
Can you tell us if you are calling unmanaged code? If so are you using default Marshalling or more specific stuff? From the looks of the stack trace are you using unsafe code e.g. Pointers and the like? This could be your problem.
您能告诉我们您是否正在调用非托管代码吗?如果是这样,您是否使用默认编组或更具体的东西?从堆栈跟踪的外观来看,您是否使用了不安全的代码,例如指针等?这可能是你的问题。
回答by Adam Lerman
Here is a more detailed stacktrace. It looks to me like it has something to do with the System.Windows.Form.dll
这是更详细的堆栈跟踪。在我看来它与 System.Windows.Form.dll 有关
the TargetSite is listed as {IntPtr DispatchMessageW(MSG ByRef)}
and under module it has System.windows.forms.dll
TargetSite 被列为{IntPtr DispatchMessageW(MSG ByRef)}
并且在它有 System.windows.forms.dll 的模块下