我正在尝试解决一个麻烦的问题,涉及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语句中)。
有人知道如何捕获(或更好的是避免)这种类型的异常吗?
geo.GetPoint(pid, **ref** v);
但你必须更改GetPoint
的声明(在你的包装器代码中,如果它不是你的话,你会有麻烦)... - EZI