How to fix 'System.StackOverflowException?

12,541

My problem is now solved. Solution: Increasing the size of the stack

Thanks to all!

Share:
12,541
FWin
Author by

FWin

Updated on June 04, 2022

Comments

  • FWin
    FWin almost 2 years

    I have a project programmed in C# NET 3.5 in WPF which start very well in debug. However for example, in my TemplateColor.cs class I have this:

    using System;
    using System.Windows.Media;
    
    namespace EWIN_THEME_MAKER.ThemeMaker
    {
        public class TemplateColor
        {
            private int _id;
    
            private string _name;
    
            private string _toneName;
    
            public int ID
            {
                get
                {
                    return this._id;
                }
                set
                {
                    this._id = value;
                }
            }
    
            public int AllVerticalPos
            {
                get;
                set;
            }
    
            public int AllHorizontalPos
            {
                get;
                set;
            }
    
            public int HuePageNum
            {
                get;
                set;
            }
    
            public int HuePos
            {
                get;
                set;
            }
    
            public int TonePaneNum
            {
                get;
                set;
            }
    
            public int TonePos
            {
                get;
                set;
            }
    
            public string Name
            {
                get
                {
                    return this._name;
                }
                set
                {
                    this._name = value;
                    string[] array = this._name.Split(new char[]
                    {
                        '_'
                    });
                    this._toneName = array[0];
                    if (array.Length > 1)
                    {
                        this.HuePageNum = int.Parse(array[1]);
                    }
                }
            }
    
            public string ToneName
            {
                get
                {
                    return this._toneName;
                }
                set
                {
                    this._toneName = value;
                }
            }
    
            public Color DarkColor
            {
                get;
                set;
            }
    
            public Color MainColor
            {
                get;
                set;
            }
    
            public Color LightColor
            {
                get;
                set;
            }
    
            public Color ShadowColor
            {
                get;
                set;
            }
    
            public byte ShadowA
            {
                get;
                set;
            }
    
            public Color GrowColor
            {
                get;
                set;
            }
    
            public Color TextShadowColor
            {
                get;
                set;
            }
    
            public Color TextMainColor
            {
                get;
                set;
            }
    
            public Color TextSelectColor
            {
                get;
                set;
            }
    
            public double TextShadowPosition
            {
                get;
                set;
            }
    
            public Brush DarkColorBrush
            {
                get
                {
                    return new SolidColorBrush(this.DarkColor);
                }
            }
    
            public Brush MainColorBrush
            {
                get
                {
                    return new SolidColorBrush(this.MainColor);
                }
            }
    
            public Brush LightColorBrush
            {
                get
                {
                    return new SolidColorBrush(this.LightColor);
                }
            }
    
            public Brush ShadowColorBrush
            {
                get
                {
                    return new SolidColorBrush(this.ShadowColor);
                }
            }
    
            public Brush GrowColorBrush
            {
                get
                {
                    return new SolidColorBrush(this.GrowColor);
                }
            }
    
            public Brush TextShadowColorBrush
            {
                get
                {
                    return new SolidColorBrush(this.TextShadowColor);
                }
            }
    
            public Brush TextMainColorBrush
            {
                get
                {
                    return new SolidColorBrush(this.TextMainColor);
                }
            }
    
            public Brush TextSelectColorBrush
            {
                get
                {
                    return new SolidColorBrush(this.TextSelectColor);
                }
            }
    
            public TemplateColor()
            {
                this.ID = -1;
                this.AllVerticalPos = -1;
                this.AllHorizontalPos = -1;
                this.HuePageNum = -1;
                this.HuePos = -1;
                this.TonePaneNum = -1;
                this.TonePos = -1;
                this.Name = "---";
                this.DarkColor = default(Color);
                this.MainColor = default(Color);
                this.LightColor = default(Color);
                this.ShadowColor = default(Color);
                this.ShadowA = 0;
                this.GrowColor = default(Color);
                this.TextShadowColor = default(Color);
                this.TextMainColor = default(Color);
                this.TextSelectColor = default(Color);
                this.TextShadowPosition = 0.0;
            }
        }
    }
    

    In this code, there are multiple errors of this kind:

    An exception of type 'System.StackOverflowException

    For example I take this part of the code there:

    public string Name
    {
        get
        {
            return this._name;
        }
        set
        {
            this._name = value;
            string[] array = this._name.Split(new char[]
            {
                '_'
            });
            this._toneName = array[0];
            if (array.Length > 1)
            {
                this.HuePageNum = int.Parse(array[1]);
            }
        }
    }
    

    So, in the next lines of this code there is an infinite call? I do not know how to correct this error.

    string[] array = this._name.Split(new char[]
    

    And...

    this.HuePageNum = int.Parse(array[1]);
    

    How do we predict and correct these errors? Yes

    • Mike Nakis
      Mike Nakis almost 7 years
      "For example I take this line there:" that's not a line. That's the code of a getter and a setter in its entirety. You need to pick one StackOverflowException and show the exact lines involved.
    • CrudaLilium
      CrudaLilium almost 7 years
      Stackoverflow occurs when there is kind of recursion in your code, there is none in the code you provided so that class is not the cause of the problem. You should look at the callstack and see a repeating pattern of calls, the problem is like in one of them.
    • FWin
      FWin almost 7 years
      @MikeNakis Ah! Thank you for your correction, I corrected.
    • FWin
      FWin almost 7 years
      Re, what code are you talking about? The complete code of my class? I will try that. @CrudaLilium
    • FinnTheHuman
      FinnTheHuman almost 7 years
      The call stack looks huge. There's probably some cyclic pattern of infinite recursion somewhere in your code, but if you're sure there isn't, you should try increasing the size of the stack: content.atalasoft.com/h/i/…
    • Lajos Arpad
      Lajos Arpad almost 7 years
      this.HuePageNum seems to be a normal property and array[1] should not result in StackOverflow. Can you put a breakpoint to the problematic line and see what is being called? I think we need a more detailed description of your exact issue.
    • FWin
      FWin almost 7 years
      @FinnTheHuman I will try...
    • CrudaLilium
      CrudaLilium almost 7 years
      I meant that code of your class TemplateColor is fine, that problem is somewhere else is your codebase. From Callstack you provided one can see there are recursive calls between ThemeMaker.GetDefaultTextureList and RootWindow constructor. The problem is probably inside GetDefaultTextureList since it creates new of instance of RootWindow and should not.
    • FWin
      FWin almost 7 years
      I'm going to try everything here, step by step, but I think @CrudaLilium is right and it's another part or a problem of Stack size. Because in the callstack there are many
    • Mike Nakis
      Mike Nakis almost 7 years
      What @CrudaLilium said.