【发布时间】:2021-01-14 02:39:13
【问题描述】:
我的 MonoGame 应用程序在 Visual Studio 中单步执行代码时输出错误,但在运行代码时却没有。我很困惑,想知道我是否违反了一些规则。所以,这里是有问题的代码:
public class SpacialHash<T>
{
public Vector2 Size, CellSize;
public HashCell<T>[,] Cells;
public SpacialHash(Vector2 size, Vector2 cellsize)
{
Size = size;
CellSize = cellsize;
Vector2 hashSize = new Vector2((int)Math.Ceiling(size.X / cellsize.X), (int)Math.Ceiling(size.Y / cellsize.Y));
Cells = new HashCell<T>[(int)hashSize.X, (int)hashSize.Y];
for (int x = 0; x < hashSize.X; x++)
{
for (int y = 0; y < hashSize.Y; y++)
{
Cells[x, y] = new HashCell<T>(this);
}
}
}
public HashCell<T> AddMember(Vector2 position, T member)
{
Vector2 cpos = new Vector2((int)Math.Floor(position.X / CellSize.X), (int)Math.Floor(position.Y / CellSize.Y));
//Breakpoint is here
if (cpos.X < Cells.GetLength(0) && cpos.Y < Cells.GetLength(1) && cpos.X >= 0 && cpos.Y >= 0)
{
//The following line of code causes the error while stepping through code
HashCell<T> cell = Cells[cpos.X, cpos.Y];
cell.AddMember(member);
return cell;
}
return null;
}
}
该函数应该找到成员应该在其中的单元格(通过计算 cpos),这似乎可以正常工作。程序检查 cpos 以确保它在界限内,然后声明单元对象,将其分配给存储在数组中 cpos 的单元的值。执行此操作的代码行会引发以下错误:
System.ExecutionEngineException
HResult=0x80131506
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
我什至不知道从哪里开始调试它。奇怪的是,运行代码时不会抛出任何错误,只是在单步执行时。那么,这种行为会不会是简单地使用带有泛型类的多维数组引起的?
谢谢,
奥兹
【问题讨论】:
-
这里的任何答案有帮助吗? stackoverflow.com/questions/3445219/…
-
我检查了这些答案,我没有检查优化代码,我清理并重建了我的解决方案,没有任何变化。
标签: c# visual-studio