繁体   English   中英

GraphicsCreator()。FillEllipse处的StackOverflow异常

[英]StackOverflow Exception at GraphicsCreator().FillEllipse

我在填充椭圆不仅是一个椭圆而且是多个椭圆时,我遇到了stackoverflow异常。

我不认为这是图形创建者的问题。 但是我不知道为什么调试器将FillEllipse命令指向stackoverflow异常

    public void createPath(Stance currentStance)
    {

        if(toSort.Count > 0)
        {
            toSort.Remove(currentStance);
            counter++;
        }

        this.currentForm.FillEllipse(new SolidBrush(Color.Red),new Rectangle(currentStance.location.X-3, currentStance.location.Y - 3, 6 , 6));

        foreach(Stance subStance in currentStance.childStances)
        {
            double weight = level(currentStance, subStance, 1)+ currentStance.StanceWeight;
            if (subStance.StanceWeight == -99999999999999)
            {
                currentStance.dajkstrasChildren.Add(subStance);
                subStance.parentStance = currentStance;
                subStance.StanceWeight = weight;
                toSort.Add(subStance);
            }
            else
            {
                if(weight > subStance.StanceWeight)
                {
                    try
                    {
                        subStance.parentStance.dajkstrasChildren.Remove(subStance);
                    }
                    catch (NullReferenceException e)
                    {
                        Console.WriteLine("null reference");
                    }
                    subStance.parentStance = currentStance;
                    currentStance.dajkstrasChildren.Add(subStance);
                    subStance.StanceWeight = weight;

                }
            }
        }

        foreach(Stance subStance in currentStance.secondChildStances)
        {         
            double weight = level(currentStance, subStance, 1) + currentStance.StanceWeight;
            if (subStance.StanceWeight == -99999999999999)
            {
                currentStance.dajkstrasChildren.Add(subStance);
                subStance.parentStance = currentStance;
                toSort.Add(subStance);
                subStance.StanceWeight = weight;
            }
            else
            {
                if (weight > subStance.StanceWeight)
                {   
                    if(subStance.parentStance != null)
                    {
                        try
                        {
                            subStance.parentStance.dajkstrasChildren.Remove(subStance);
                            subStance.parentStance = currentStance;
                            currentStance.dajkstrasChildren.Add(subStance);
                        }
                        catch(NullReferenceException e)
                        {
                            Console.WriteLine("null reference");
                        }
                    }

                }
            }
        }

        toSort.Sort(new Stance());
        if(toSort.Count != 0)
        {
            createPath((Stance)toSort[0]);
        }
    }

它是一种递归方法,但是它不能递归到无穷大,因为它总是从toSort ArrayList中弹出单个对象

这是因为试图对FillEllipse进行调用实际上会使堆栈耗尽。

当然,堆栈溢出必须由逻辑缺陷引起,该逻辑缺陷导致(可能)无限期地或太深地递归调用createPath方法,对于堆栈而言,其无法容纳所有所需的激活框架。

使用while循环而不是递归来避免加载堆栈。

这是修改后的代码,可能会起作用:

public void createPath(Stance stance)
    {
        var currentStance = stance;

        while(toSort.Count >0)
        {
            toSort.Remove(currentStance);
            counter++;


        this.currentForm.FillEllipse(new SolidBrush(Color.Red),new Rectangle(currentStance.location.X-3, currentStance.location.Y - 3, 6 , 6));

        foreach(Stance subStance in currentStance.childStances)
        {
            double weight = level(currentStance, subStance, 1)+ currentStance.StanceWeight;
            if (subStance.StanceWeight == -99999999999999)
            {
                currentStance.dajkstrasChildren.Add(subStance);
                subStance.parentStance = currentStance;
                subStance.StanceWeight = weight;
                toSort.Add(subStance);
            }
            else
            {
                if(weight > subStance.StanceWeight)
                {
                    try
                    {
                        subStance.parentStance.dajkstrasChildren.Remove(subStance);
                    }
                    catch (NullReferenceException e)
                    {
                        Console.WriteLine("null reference");
                    }
                    subStance.parentStance = currentStance;
                    currentStance.dajkstrasChildren.Add(subStance);
                    subStance.StanceWeight = weight;

                }
            }
        }

        foreach(Stance subStance in currentStance.secondChildStances)
        {         
            double weight = level(currentStance, subStance, 1) + currentStance.StanceWeight;
            if (subStance.StanceWeight == -99999999999999)
            {
                currentStance.dajkstrasChildren.Add(subStance);
                subStance.parentStance = currentStance;
                toSort.Add(subStance);
                subStance.StanceWeight = weight;
            }
            else
            {
                if (weight > subStance.StanceWeight)
                {   
                    if(subStance.parentStance != null)
                    {
                        try
                        {
                            subStance.parentStance.dajkstrasChildren.Remove(subStance);
                            subStance.parentStance = currentStance;
                            currentStance.dajkstrasChildren.Add(subStance);
                        }
                        catch(NullReferenceException e)
                        {
                            Console.WriteLine("null reference");
                        }
                    }

                }
            }
        }

        toSort.Sort(new Stance());
        currentStance = (Stance)toSort[0];
      }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM