GraphicsCreator()中的StackOverflow异常.FillEllipse

时间:2013-04-03 18:03:18

标签: c# stack-overflow

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

我不认为这是图形创建者的问题。但我无法弄清楚为什么调试器指向FillEllipse命令

的堆栈溢出异常
    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中弹出一个对象

2 个答案:

答案 0 :(得分:0)

那是因为它试图调用FillEllipse,实际上堆栈实际用完了。

当然,堆栈溢出必须由您的逻辑中的缺陷引起,该缺陷导致您的createPath方法被递归地(可能)无限地或过深地调用,以便堆栈能够容纳所有需要的激活帧。

答案 1 :(得分:0)

使用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];
      }
    }