【问题标题】:How to fix 'System.StackOverflowException?如何修复'System.StackOverflowException?
【发布时间】:2017-09-27 09:04:44
【问题描述】:

我有一个在 WPF 中用 C# NET 3.5 编写的项目,它在调试中启动得很好。但是,例如,在我的TemplateColor.cs 课程中,我有这个:

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

在这段代码中,有多个此类错误:

'System.StackOverflowException 类型的异常

例如我把这部分代码放在那里:

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

那么,在这段代码的下一行中有一个无限调用吗?我不知道如何纠正这个错误。

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

还有……

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

我们如何预测和纠正这些错误?

【问题讨论】:

  • "例如我把这条线放在那里:" 那不是一条线。这就是完整的 getter 和 setter 的代码。您需要选择一个 StackOverflowException 并显示所涉及的确切行。
  • 当您的代码中存在某种递归时会发生 Stackoverflow,您提供的代码中没有递归,因此该类不是问题的原因。您应该查看调用堆栈并看到重复的调用模式,问题就像其中一个一样。
  • @MikeNakis 啊!谢谢你的指正,我更正了。
  • Re,你在说什么代码?我班级的完整代码?我会试试的。 @CrudaLilium
  • 我的意思是你的类 TemplateColor 的代码很好,问题出在其他地方是你的代码库。从您提供的 Callstack 可以看到 ThemeMaker.GetDefaultTextureList 和 RootWindow 构造函数之间存在递归调用。问题可能出在 GetDefaultTextureList 内部,因为它创建了新的 RootWindow 实例并且不应该。

标签: c# .net wpf .net-3.5 stack-overflow


【解决方案1】:

我的问题现在解决了。 解决方案:Increasing the size of the stack

谢谢大家!

【讨论】:

    猜你喜欢
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多