1

当我填充椭圆时,我得到一个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 中弹出一个对象

4

2 回答 2

0

那是因为在尝试调用 FillEllipse 时堆栈实际上用完了。

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

于 2013-04-03T18:14:43.980 回答
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];
      }
    }
于 2013-04-03T18:31:58.977 回答