Access violation exception when use method Marshal.PtrToStructure in a loop

白昼怎懂夜的黑 提交于 2019-12-06 13:02:35

When i change some code, access violation does not occur. However, i don't understand root cause of this problem. What access violation exception occur?

Code modify as bellow:

private ArrayList DoPolygonOperation()
{
  IntPtr currentVertex = vertexList.vertexes;

  gpc_vertex oVertext = new gpc_vertex();
  int currentOffset = 0; 
  for (int j = 0; j < vertexList.num_vertices; j++)
  {
    PositionF pos = new PositionF();
    oVertext = (gpc_vertex)Marshal.PtrToStructure((IntPtr)(currentVertex.ToInt64() + currentOffset), typeof(gpc_vertex));
    pos.X = oVertext.x;
    pos.Y = oVertext.y;
    Marshal.DestroyStructure(currentVertex, typeof(gpc_vertex));
    currentOffset += Marshal.SizeOf(oVertext);

    posList.Add(pos);
 }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!