StackOverflowException on Getter C#

后端 未结 3 1706
旧巷少年郎
旧巷少年郎 2021-01-19 06:41

I am getting a StackOverflowException on the get; of a property in an abstract class.

public abstract class SenseHatSnake
    {

        privat         


        
3条回答
  •  不思量自难忘°
    2021-01-19 07:21

    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.

提交回复
热议问题