C# Debugging Error Only When Stepping Through Code

-1

My MonoGame application is outputting an error while stepping through code in visual studio, but not while running the code. I am stumped and wondering if I have been breaking some rules. So, here is the code in question:

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

The function is supposed to find the cell the member is supposed to be within (by calculating cpos), which seems to work properly. The program checks cpos to ensure it is within bounds, then declares the cell object, assigning it to the value of the cell stored at cpos in the array. The line of code which does this throws the following error:

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

Error

I am at a loss for where to even begin to debug this. The strange part about this, is that no errors are thrown when running the code, just when stepping through it. So, could this behavior be caused by simply using a multidimensional array with generic classes?

Thank you,

Ozzie

c#
visual-studio
asked on Stack Overflow Jan 14, 2021 by OzGorb • edited Jan 14, 2021 by OzGorb

1 Answer

0

I'm guessing that it is binding the wrong version of the .net assembly, because it cannot even find the exception type (according to the error message you are showing).

You can determine better if there is a failure by using the Fusion Log Viewer (fuslogvw.exe). Fusion was the original codename for .NET before it released.

The easiest way to get to the app is to start up a Visual Studio command prompt (make sure you run it as Admin - or you won't be able to make changes) and type

fuslogvw <ENTER>

fuslogvw

It's a very ugly utility but it can help you determine if a .NET binding is failing.

You can see more about how to set it up at: https://docs.microsoft.com/en-us/dotnet/framework/tools/fuslogvw-exe-assembly-binding-log-viewer

Click [Settings] and set it up like the following:

log all binds to disk

Now it'll write values to the fusion log whenever .NET binds a library. Go back to your program and run it in debug mode and step through the code til it fails.

When it fails come back to Fusion Log Viewer and click the View Log button.
Hopefully you'll see the version of the .NET DLL that it is binding to. It may be a different version that what you're getting for non-debug runs.

IMPORTANT

Don't forget to set the Settings back to [Log Disabled] when you are all done or else all .NET programs will write to the log system-wide and slow your computer down.

answered on Stack Overflow Jan 14, 2021 by raddevus

User contributions licensed under CC BY-SA 3.0