zoukankan      html  css  js  c++  java
  • 未处理AccessViolationException 异常

     在进行arcgis的GP操作时,当操作栅格图像的拼接时,报错:

    AccessViolationException: 尝试读取或写入受保护的内存

     原以为可以通过try catch屏蔽掉错误,不至于程序崩溃,但是,catch不起作用。不知道为什么?后来google才发现。问题:大致意思是Net能处理托管的Structured Error Handling (SEH)异常,

    但是对于非托管Corrupted State Exceptions (CSE)异常却不能捕获,如果想捕获此种类型异常,需要在配置文件中,添加一下内容

      <runtime>
        <legacyCorruptedStateExceptionsPolicy enabled="true" />
      </runtime>

    在try catch中添加了AccessViolationException的捕获,代码如下:

      private static bool BeginRunGP(IGPProcess process, ITrackCancel TC, Action<string> pMessage)
            {
                bool result = false;
                try
                {
    
                    if (m_geoprocessor == null)
                    {
                        m_geoprocessor = new Geoprocessor();
                    }
    
                    m_geoprocessor.OverwriteOutput = true;
                    //m_geoprocessor.MessagesCreated += m_geoprocessor_MessagesCreated;
                    //showMessage("-------------------------------------------------------------------------------------------------------" + Environment.NewLine);
                    //showMessage(string.Format("开始时间:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")) + Environment.NewLine);
                    if (pMessage != null)
                    {
                        pMessage("GP:" + process.ToolName);
                    }
                    IGeoProcessorResult2 geoProcessorResult = m_geoprocessor.Execute(process, null) as IGeoProcessorResult2;
                    if (geoProcessorResult != null && geoProcessorResult.Status == esriJobStatus.esriJobSucceeded)
                    {
                        result = true;
                    }
    
                    if (geoProcessorResult != null)
                    {
                        for (int i = 0; i < geoProcessorResult.MessageCount; i++)
                        {
                            string str = geoProcessorResult.GetMessages(i);
                            if (pMessage != null)
                            {
                                pMessage(str);
                            }
                        }
                    }
                    if (pMessage != null)
                    {
                        pMessage("GP:" + process.ToolName + " 结束");
                    }
                    result = true;
                }
                catch (System.AccessViolationException ex)
                { //   AccessViolationException异常捕获
                    result = false;
                }
                catch (Exception ex)     //   COMException
                {
                    result = false;
                    if (pMessage != null)
                    {
                        pMessage("GP:" + process.ToolName + " 错误");
                        pMessage(ex.Message);
                        LogClass.PubLog.ErrorFormat("GP:{0} 错误,错误原因是:{1}", process.ToolName, ex.Message);
                    }
                    LogClass.PubLog.ErrorFormat("GP:{0} 错误,错误原因是:{1}", process.ToolName, ex.Message);
                }
                finally
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    for (int i = 0; i < m_geoprocessor.MessageCount; i++)
                        sb.AppendLine(m_geoprocessor.GetMessage(i));
    
                }
                return result;
            }

    下面是他的英文解释

     
    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/magazine/dd419661.aspx#id0070035

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

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

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

    (3)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.

    For more reference:http://connect.microsoft.com/VisualStudio/feedback/details/557105/unable-to-catch-accessviolationexception

  • 相关阅读:
    Asp.Net微型服务器,只有一个文件,并且才300K大小|建议从事Asp.Net开发的博友们人手一份 狼人:
    “Asp.Net微型服务器”根据博友们的要求改版了,也出.NET4.0版本了,要更新的博友们赶快下吧 狼人:
    不经历风雨,怎么见彩虹,没有人能随随便便成功 狼人:
    C#汉字转拼音代码分享|建议收藏 狼人:
    用C#开发类似QQ输入法的不规则窗体的程序详解+代码打包分享 狼人:
    Android开发必备武器,处理X“.NET研究”ML的利器——SAX快速上手 狼人:
    “.NET研究”在iPhone应用中如何避免内存泄露 狼人:
    向量样本【模式识别】感知器 Perceptron
    编程在线庞果网 在线编程 24点游戏
    实现接口一种可靠的 DLL 接口实现方案
  • 原文地址:https://www.cnblogs.com/cglNet/p/6913024.html
Copyright © 2011-2022 走看看