如何处理 AccessViolationException [英] How to handle AccessViolationException

查看:31
本文介绍了如何处理 AccessViolationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 .net 应用程序中使用 COM 对象 (MODI).我调用的方法抛出一个 System.AccessViolationException,它被 Visual Studio 拦截.奇怪的是,我将调用封装在 try catch 中,它具有用于 AccessViolationExceptionCOMException 和其他所有内容的处理程序,但是当 Visual Studio (2010) 拦截 AccessViolationExceptionCOMExceptioncode>AccessViolationException,调试器在方法调用(doc.OCR)上中断,如果我单步执行,它会继续到下一行而不是进入 catch 块.此外,如果我在 Visual Studio 之外运行它,我的应用程序就会崩溃.我该如何处理在 COM 对象中抛出的这个异常?

I am using a COM object (MODI) from within my .net application. The method I am calling throws a System.AccessViolationException, which is intercepted by Visual Studio. The odd thing is that I have wrapped my call in a try catch, which has handlers for AccessViolationException, COMException and everything else, but when Visual Studio (2010) intercepts the AccessViolationException, the debugger breaks on the method call (doc.OCR), and if I step through, it continues to the next line instead of entering the catch block. Additionally, if I run this outside of the visual studio my application crashes. How can I handle this exception that is thrown within the COM object?

MODI.Document doc = new MODI.Document();
try
{
    doc.Create(sFileName);
    try
    {
        doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false);
        sText = doc.Images[0].Layout.Text;
    }
    catch (System.AccessViolationException ex)
    {
        //MODI seems to get access violations for some reason, but is still able to return the OCR text.
        sText = doc.Images[0].Layout.Text;
    }
    catch (System.Runtime.InteropServices.COMException ex)
    {
        //if no text exists, the engine throws an exception.
        sText = "";
    }
    catch
    {
        sText = "";
    }

    if (sText != null)
    {
        sText = sText.Trim();
    }
}
finally
{
    doc.Close(false);

    //Cleanup routine, this is how we are able to delete files used by MODI.
    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(doc);
    doc = null;
    GC.WaitForPendingFinalizers();
    GC.Collect();
    GC.WaitForPendingFinalizers();

}

推荐答案

在 .NET 4.0 中,运行时处理某些作为 Windows 结构化错误处理 (SEH) 错误引发的异常,作为损坏状态的指示器.您的标准托管代码不允许捕获这些损坏状态异常 (CSE).我不会讨论这里的原因或方式.阅读这篇关于 .NET 4.0 Framework 中 CSE 的文章:

In .NET 4.0, the runtime handles certain exceptions raised as Windows Structured Error Handling (SEH) errors as indicators of Corrupted State. These Corrupted State Exceptions (CSE) are not allowed to be caught by your standard managed code. I won't get into the why's or how's here. Read this article about CSE's in the .NET 4.0 Framework:

http://msdn.microsoft.com/en-us/杂志/dd419661.aspx#id0070035

但还是有希望的.有几种方法可以解决这个问题:

But there is hope. There are a few ways to get around this:

  1. 重新编译为 .NET 3.5 程序集并在 .NET 4.0 中运行.

  1. Recompile as a .NET 3.5 assembly and run it in .NET 4.0.

在配置/运行时元素下的应用程序配置文件中添加一行:

Add a line to your application's config file under the configuration/runtime element: <legacyCorruptedStateExceptionsPolicy enabled="true|false"/>

使用 HandleProcessCorruptedStateExceptions 属性修饰您想要捕获这些异常的方法.请参阅 http://msdn.microsoft.com/en-us/magazine/dd419661.aspx#id0070035 了解详情.

Decorate the methods you want to catch these exceptions in with the HandleProcessCorruptedStateExceptions attribute. See http://msdn.microsoft.com/en-us/magazine/dd419661.aspx#id0070035 for details.

<小时>

编辑

之前,我参考了一个论坛帖子 了解更多详情.但由于 Microsoft Connect 已停用,以下是您感兴趣的其他详细信息:

Previously, I referenced a forum post for additional details. But since Microsoft Connect has been retired, here are the additional details in case you're interested:

来自 Microsoft CLR 团队的开发人员 Gaurav Khanna

From Gaurav Khanna, a developer from the Microsoft CLR Team

这种行为是由于 CLR 4.0 的一项称为损坏状态异常的功能而设计的.简而言之,托管代码不应该尝试捕获表明进程状态损坏的异常,而 AV 就是其中之一.

This behaviour is by design due to a feature of CLR 4.0 called Corrupted State Exceptions. Simply put, managed code shouldnt make an attempt to catch exceptions that indicate corrupted process state and AV is one of them.

然后他继续参考 上的文档HandleProcessCorruptedStateExceptionsAttribute 和上面的文章.可以说,如果您正在考虑捕获这些类型的异常,那绝对值得一读.

He then goes on to reference the documentation on the HandleProcessCorruptedStateExceptionsAttribute and the above article. Suffice to say, it's definitely worth a read if you're considering catching these types of exceptions.

这篇关于如何处理 AccessViolationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆