簡體   English   中英

C# 僅在單步執行代碼時出現調試錯誤

[英]C# Debugging Error Only When Stepping Through Code

我的 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;
        }
    }

function 應該找到成員應該在其中的單元格(通過計算 cpos),這似乎工作正常。 該程序檢查 cpos 以確保它在范圍內,然后聲明單元格 object,將其分配給存儲在數組中 cpos 的單元格的值。 執行此操作的代碼行會引發以下錯誤:

System.ExecutionEngineException
  HResult=0x80131506
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

錯誤

我不知道從哪里開始調試它。 奇怪的是,運行代碼時不會拋出任何錯誤,只是單步執行。 那么,這種行為是否可能是由簡單地使用具有泛型類的多維數組引起的?

謝謝,

奧齊

我猜它綁定了錯誤版本的 .net 程序集,因為它甚至找不到異常類型(根據您顯示的錯誤消息)。

您可以使用 Fusion 日志查看器 (fuslogvw.exe) 更好地確定是否存在故障。 Fusion 發布前的原始代號為 .NET。

訪問該應用程序的最簡單方法是啟動 Visual Studio 命令提示符(確保以管理員身份運行它 - 否則您將無法進行更改)並鍵入

fuslogvw <ENTER>

fuslog大眾

這是一個非常丑陋的實用程序,但它可以幫助您確定 .NET 綁定是否失敗。

您可以在以下位置查看有關如何設置它的更多信息: https://learn.microsoft.com/en-us/do.net/framework/tools/fuslogvw-exe-assembly-binding-log-viewer

點擊【設置】,進行如下設置:

記錄所有綁定到磁盤

現在,只要 .NET 綁定庫,它就會將值寫入融合日志。 Go 回到您的程序並在調試模式下運行它並逐步執行代碼直到失敗。

當它失敗時返回 Fusion Log Viewer 並單擊 View Log 按鈕。
希望您會看到它綁定到的 .NET DLL 的版本。 它可能與您為非調試運行獲得的版本不同。

重要的

完成后不要忘記將設置設置回 [禁用日志],否則所有 .NET 程序將在系統范圍內寫入日志並降低計算機速度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM