如何在C#中捕获'FatalExecutionEngineError'?

3
我正在尝试解决一个麻烦的问题,涉及C#例程。我正在开发一个应用程序,使用GDAL库打开ESRI ShapeFile(一种用于操作地理数据的C++ DLL库),并在PictureBox组件中显示地图。当我使用System.Drawing.Point对象的向量来绘制多边形时,会收到以下消息: 托管调试助手“FatalExecutionEngineError”在“C:\Users\polli\ipeageo-git\IpeaGEO\bin\Debug\IpeaGEO.vshost.exe”中检测到问题。 额外信息:运行时发现致命错误。错误地址为0x6ced9a0f,在线程0x1618中。错误代码为0xc0000005。 以下是引发异常的代码:
private void drawGeometry(Geometry geo, Graphics g, bool fill)
{
    // Some code here...
    // ...

    // Get the points count and the array to ask GDAL for coordinates.
    int count = geo.GetPointCount();
    double[] v = new double[2];
    Point[] polygon = new Point[count];

    for (int pid = 0; pid < count; pid++)
    {
        geo.GetPoint(pid, v);    // This is a call to GDAL (unmanaged) code.
        polygon[pid].X = getX((float)v[0]);
        polygon[pid].Y = getY((float)v[1]);

        // The Exception occurs just HERE!
        g.DrawPolygon(fgPen, polygon); // <--- EXCEPTION!!!
        if (fill) g.FillPolygon(fillBrush, polygon);
    }

    // Some code here...
    // ...
}

我有另一个版本的这个函数,它可以正常工作,在该版本中,我绘制每个线段而不分配内存:

private void drawGeometry(Geometry geo, Graphics g, bool fill)
{
    // Some code here...
    // ...

    Point start = new Point(), current = new Point(), previous = new Point();

    // Get the points count and the array to ask GDAL for coordinates.
    int count = geo.GetPointCount();
    double[] v = new double[2];

    for (int pid = 0; pid < count; pid++)
    {
        geo.GetPoint(pid, v);    // This is a call to GDAL (unmanaged) code.
        if (pid == 0)
        {
            start.X = previous.X = getX((float)v[0]);
            start.Y = previous.Y = getY((float)v[1]);
         } // if
         else
         {
             previous.X = current.X;
             previous.Y = current.Y;
         } // else

         current.X = getX((float)v[0]); current.Y = getY((float)v[1]);
         g.DrawLine(fgPen, previous.X, previous.Y, current.X, current.Y);
    } // for
    g.DrawLine(fgPen, start.X, start.Y, current.X, current.Y);

    // Some code here...
    // ...
}

我需要填充一些多边形,但是使用第二个版本的代码时无法实现(第二个版本的代码能够正常工作)。try ... catch 没有捕获到异常。

我认为问题出现在当 垃圾回收器 在后台运行时,我尝试访问多边形变量(这是一个约2000个元素的向量...并且此代码位于一个for语句中)。

有人知道如何捕获(或更好的是避免)这种类型的异常吗?


2
我觉得你误解了那个异常的名称。它被称为“fatal”的原因是因为它是致命的,进程中没有希望继续执行,注定要失败。唯一的解决方案是找出为什么会发生这种情况,并修复它,但你不能捕获它。 - Lasse V. Karlsen
问题在于我无法避免异常的发生。我需要找到一种方法来避免与垃圾收集器竞争。当我访问__polygon__向量时出现问题(我认为它可能被垃圾收集器锁定)。 - Demerson Polli
我不认为这是垃圾回收问题。我猜测你的 C++ 库需要 geo.GetPoint(pid, **ref** v); 但你必须更改 GetPoint 的声明(在你的包装器代码中,如果它不是你的话,你会有麻烦)... - EZI
你应该展示dll中的声明以及你在C#中如何声明它。 - Lasse V. Karlsen
问题在于你必须避免它,因为你根本无法捕获和处理那个异常。某些事情出了大问题,进程将被终止,这是无法避免的。所以你需要想办法在第一时间避免异常的发生。 - Lasse V. Karlsen
显示剩余2条评论
1个回答

1

FatalExecutionEngineError 无法在异常处理中捕获,它们通常是系统错误或封送错误,两者都无法在 .NET 应用程序中处理。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接