gpt4 book ai didi

C# Winforms : Debug strategies to find cause for System. AccessViolationException 异常

转载 作者:行者123 更新时间:2023-12-04 18:05:14 26 4
gpt4 key购买 nike

在我的 winforms 应用程序中,我随机遇到以下异常:

Application: My.Shell.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
Stack:
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG ByRef)
at System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr, Int32, Int32)
at System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext)
at System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext)
at System.Windows.Forms.Application.Run(System.Windows.Forms.Form)
at Microsoft.Practices.CompositeUI.WinForms.FormShellApplication`2[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Start()
at Microsoft.Practices.CompositeUI.CabApplication`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Run()
at My.Shell.ModuleLoader.Main()

堆栈跟踪来自 Windows 事件查看器。 (unandeled 异常处理程序没有得到它。)

我在 SO 上阅读了很多关于此的文章:

事实证明,我的应用程序在某处通过 native 方法访问已处置或损坏的内存。我现在的问题是如何使用我用任务管理器制作的故障转储找到该位置。是否有其他策略可以找到原因?

最佳答案

我发现 UnhandledExceptionThreadException 事件通常不会在 WinForms 中触发。在我的应用程序中,我使用三种不同的方法来捕获未处理的异常。通常它只是获取它的 try catch

static void Main(string[] args)
{
Application.ThreadException += Application_ThreadException;
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

try
{
var mform = new MainForm();
Application.Run(mform);
}
catch (Exception ex)
{
HandleException(ex);
}
}

private void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
HandleException(e.Exception);
}

private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
HandleException(e.ExceptionObject as Exception);
}

private void HandleException(Exception ex)
{
// Do something with it
}

关于C# Winforms : Debug strategies to find cause for System. AccessViolationException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28367195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com