I am getting a StackOverflowException on the get;
of a property in an abstract class.
public abstract class SenseHatSnake
{
privat
The origin of the overflow has been identified in other answers, and other answers have pointed out the architectural design problems in your program sketch.
I thought I'd say briefly how to debug such issues yourself.
First: when faced with a stack overflow, there is almost always an unbounded recursion. When the overflow is reported in a member that plainly has no stack overflow, what has happened? This has happened:
void Bad()
{
Good();
Bad();
}
If Bad
is called then it will stack overflow, but most of the time the overflow will be reported in Good
, because Good
probably uses more stack than any single call to Bad
.
What you need to do is look at the call stack, because it will be Good / Bad / Bad / Bad / Bad ...
and that tells you that it is Bad
that is doing the unbounded recursion. The key to finding the source of a recursion is to find the thing that is actually calling itself, directly or indirectly. Use the instrumentation at your disposal to do so; the exception contains a trace of the call stack.